Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This particular example is 0, [1], which is easily seen by pasting it in the console.

There may be some edge cases I'm not covering here, but JavaScript passes primitives by value (strings, numbers, etc) and non-primitives (objects, arrays, etc) by passing a copy of the reference.

What this means is if you reassign the reference inside of the method, it will only affect that scope, because the reference itself is a copy. If you modify the properties of the non-primitive, it will be modified, because the copy of the reference points to the same non-primitive.

Since primitives are passed by value then any modifications are not reflected outside of the method.

To the other poster below, in JavaScript, const with an array (object, etc) simply prevents reassignment of the variable, the array can still be modified.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: