0

[solved] Can someone explain the output of this code ?

var arr1 = [7,8,9]; var arr2 = arr1; arr2[0] = 8; console.log(arr1[0]); I don't understand why arr1[0] outputs 8 after arr2[0] has been changed to 8 !?

26th Aug 2019, 11:52 PM
Peter
3 ответов
+ 1
Because of var arr2 = arr1;
26th Aug 2019, 11:53 PM
Vanwolf
Vanwolf - avatar
+ 1
Glad you figured it out Pete!
27th Aug 2019, 2:44 AM
Vanwolf
Vanwolf - avatar
0
Alright, so it seems that assigning arr1 to arr2 is not only passing arr1's values to arr2 (like it would with strings or numbers variables). It kind of links them together.. so 2 variable names for the same array.. somehow.. Still slightly confusing 😕 but I'll keep trying thanks
27th Aug 2019, 2:38 AM
Peter