Consider the following code snippet:
var arr1 = "john".split('');
console.log(arr1); // [ 'j', 'o', 'h', 'n' ]
var arr2 = arr1.reverse();
console.log(arr1); // [ 'n', 'h', 'o', 'j' ]
var arr3 = "jones".split('');
arr2.push(arr3);
console.log(arr1); // [ 'n', 'h', 'o', 'j', [ 'j', 'o', 'n', 'e', 's' ] ]
In the last console.log
, why does pushing to arr2
affect arr1
? Why does arr1
change when it's not modified, only arr2
is modified?