It's to do with the fact it's an object. newCity actually refers to the same object in memory as city does, so changing one is identical to changing the other. A better explanation.
Great wit and madness are near allied, and fine a line their bounds divide.
declan explains half the effect: the part about all objects in js are passed byRef.
the other trick is that Array.reverse is mutative, which means that it actually alters the Array it's called on. splice(), sort(), and the stack operators like push/pop also mutate.
many array methods dont: map(), filter(), slice(), join(), etc all leave the orig intact.
if you want another copy to reverse: newCity = city.slice().reverse()
i've found slice to bench slightly higher than concat in enough browsers to make it worth using slice(). plus, slice is one fewer key to press than concat...
Bookmarks