
Metadata
- Author: joshwcomeau.com
- Full Title: The “Const” Deception
- URL: https://www.joshwcomeau.com/javascript/the-const-deception/
Highlights
- When we use the
letkeyword to create a variable, we’re able to change which “thing” that label refers to. (View Highlight)
(View Highlight)- This is known as re-assignment. We’re saying that actually, the
fruitslabel should refer to an entirely different value: (View Highlight) - By contrast, variables created with
constcannot be reassigned: (View Highlight)
(View Highlight)- Here’s the thing, though: we’re still allowed to modify the data itself! As long as the label remains intact. (View Highlight)
- When we create a constant with
const, we can be 100% sure that the variable will never be re-assigned, but no promises are made when it comes to mutation.constdoesn’t block mutation at all. (View Highlight) - As far as I know,
Object.freeze()is bulletproof. There’s no way to modify an object/array that has been frozen using this method. (View Highlight) - Here’s the deal: all primitive data types in JavaScript are immutable. It’s impossible to “edit” the value of a number. We can only reassign the variable to a different value. (View Highlight)