0

can someone explain to me why the variable old is still assigned to 1 instead of 8? I thought the variable old will change to 8 because I have assigned a new value to array [0]. I thought the equal sign holds the transitivity property.

array= [1,2,3,4,5];  
var old=array[0];  
array[0]=8;  
console.log(old);//1

Thanks for your help

hikari
  • 1
  • `var old=array[0];` just sets `old` to whatever value `array[0]` has at that moment, it doesn't create any kind of ongoing link with that array element. – nnnnnn Sep 25 '16 at 05:49
  • from JS MDN - An assignment operator assigns a value to its left operand based on the value of its right operand – Tommy Sep 25 '16 at 05:51

1 Answers1

0
var old=array[0];   

Here you're assigning the value of array[0] to the variable old

This doesn't change array[0]. This basically means that this is passed by value and not by reference.

nikjohn
  • 20,026
  • 14
  • 50
  • 86