I\'m deeply confused by the behaviour of either JavaScript, or the Chrome console. Can someone help me understand?
Basically I have the following JavaScript code, no
var initial_array = [];
function initialiseArray() {
initial_array = [2, 9, 8, 6, 0, 2, 1];
}
function copyToNewArray() {
var copied_array = [];
console.log("COPIED 1", copied_array);
alert(copied_array.length);
for (var i = 0; i < initial_array.length; i++) {
var copy = initial_array[i];
copied_array.push(copy);
}
console.log("COPIED 2", copied_array);
}
initialiseArray();
copyToNewArray();
Adding the line alert(copied_array.length); will show correct results.
What happens is that the log is not synchronized with the javascript execution. When the log prints the values were already changed.