javascript-questions icon indicating copy to clipboard operation
javascript-questions copied to clipboard

Question 109 - Misleading

Open rotexhawk opened this issue 5 years ago • 0 comments

https://github.com/lydiahallie/javascript-questions#109-whats-the-output

Given the explanation in 109 the value of my original array should change because it's not a primitive value but if you test the code you will see that the original array stays the same. I think the explanation for 109 is a bit misleading because that flow has nothing to do with primitives vs non primitives.

I think when we create test we create a new object with a key that points to elm[0] and when when change the value of test object it just lets go of the pointer. Changing test will never change the original array value.


const elm = [{ dog: 'dog', cat: 'cat'}]

const test = { animal: elm[0] }

test.animal = 'what the shit'

console.log(test)

console.log(elm) //[ { dog: 'dog', cat: 'cat' } ]

rotexhawk avatar Jan 03 '20 20:01 rotexhawk