Why this code result is 4? b was defined before array a change | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why this code result is 4? b was defined before array a change

https://code.sololearn.com/W4SgQA3dTnnF/?ref=app

10th Jun 2017, 6:17 PM
Kirill Chertkov
Kirill Chertkov - avatar
3 Answers
+ 5
Because arrays are objects. console.log(typeof [1,2,3]) -->object Variables get "references" to objects (not copies). Assigning a variable to another variable that points to an object just creates two pointers to the SAME object, not two full objects. Primitive types / not objects are stored as values (and make copies): a=3 b=a // gets a copy a=0 // b is not affected console.log(a + "," + b) 0,3
10th Jun 2017, 6:39 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Kirk, thank you
10th Jun 2017, 8:20 PM
Kirill Chertkov
Kirill Chertkov - avatar
0
I cant understand your question but my ans is a=3; b=a //means b=3 a=0 //means now a=0 but not a=3 so that's why a=0 and b=3 console.log(a+"."+b)//a=0 and b=3 so 0.3 ans. here you mention point in peremiter(eg a+"."+b), if you write like (a+" "+b) then you will get 0 3. I hope your meaning is clear...
10th Jun 2017, 8:17 PM
meherDev
meherDev - avatar