When I add variables to an array in JavaScript and then update each element, why do the variables all show the same value when outputting them in the console?
In this example, I would anticipate seeing 777
for all variables in the console, but instead they display as 0.
However, when logging the array itself, it correctly shows [777,777,777]
.
var number1 = 0;
var number2 = 0;
var number3 = 0;
var numbers = [number1, number2, number3];
function updateNumbers() {
var i;
for (i = 0; i < numbers.length; i++) {
numbers[i] = 777;
console.log(numbers);
console.log(number1);
console.log(number2);
console.log(number3);
}
}
updateNumbers();