I was playing with some arrays in JavaScript when I came across something strange. Here is my code:
var origArray = new Array("one","two","three","four","five","six","seven","eight"); var newArray = origArray.slice(1,3); origArray[1] = "octopus"; console.log(newArray.join()); //prints two,three var origArray = new Array(["one","two"],["three","four"],["five","six"],["seven","eight"]); var newArray = origArray.slice(1,3); origArray[1][0] = "octopus"; console.log(newArray.join()); //prints octopus,four,five,six
I do not understand why newArray affects the second case, and not the first. What's going on here?
source share