DEV Community

Discussion on: JS fundamentals: const vs var vs let?

Collapse
pentacular profile image
pentacular • Edited on

However this behavior changes when it comes to non-primitive types (objects, arrays, etc) as const.

Actually, it doesn't change at all.

const x = y;

This means that you may not reassign x, and this is true regardless of y having a primitive of object value.

The confusion is due to conflating the properties of an object with the value of the object.

The value of the object is the object's identity, and this allows you to access the properties associated with that object.

These properties can be modified, but modifying the properties does not change the value (or identity) of the object.

non-primitive types (objects, arrays, etc) as const.

When you use const, it has nothing to do with the value -- const only affects the variable.

Collapse
christianmontero profile image
Christian Montero Author

Thanks for correcting my mistake, I really appreciate it! 👍

Collapse
pentacular profile image
pentacular

You're welcome. :)