In JavaScript, when I add variables to an array and then update each element in the array, why does the variable have the same value when output in the console?
In the below I would expect all variables to show 777 in the console but they show 0.
When the array is logged [777,777,777] is shown as expected.
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();
number1andnumbers[0]are not the same variable.