Skip to content

Ask HN: In JavaScript, changing the value of an array item not advised?

2 pointsmcdoh2 comments
On HN

I'm reading through jQuery Fundamentals (http://www.rebeccamurphey.com/jqfundamentals/#javascript-basics.examples.change-array-value) and this tip in the JavaScript chapter surprised me: While it's possible to change the value of an array item as shown in Example 2.28, “Changing the value of an array item”, it's generally not advised.

I wouldn't think just changing the value of an array element could cause a problem. Is this truly inadvisable or was this written in error?

Comments

It shouldn't be a problem with strings and numerics in arrays (the array contains copies of the values), but with arrays of functions/objects, the elements of the array are not copies, but the actual objects themselves. So if you change an object in the array, the object itself changes (and vice versa).

Thank you.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.