JavaScript const: Immutable Binding vs Immutable Value

Why const isn't actually "constant" in JavaScript 🤯 If you're new to JS, you might think const means the value can never change. But try this: const user = { name: 'Alice' }; user.name = 'Bob'; console.log(user.name); // Output: Bob Wait, what? Why didn't it throw an error? Here’s the deal: const creates an immutable binding, not an immutable value. It prevents you from reassigning the variable to a new memory address. It does NOT prevent you from changing the properties of an object or array. Key Takeaway: If you want a truly unchangeable object, use Object.freeze(). What’s a JS quirk that tripped you up when you first started? Let’s discuss below! 👇 feel free to reach me out for any career mentoring Naveen .G.R | CareerByteCode #JavaScript #WebDevelopment #CodingTips #Frontend #SoftwareEngineering

  • graphical user interface, text

const does not make objects immutable; it only prevents reassignment of the variable. Object properties can still be changed unless the object is frozen using Object.freeze().

Like
Reply

Excellent deep dive into how const works in JavaScript. It perfectly demonstrates the importance of understanding core fundamentals. Thank you for the insightful content Naveen .G.R.

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories