⚡ Tip: const Doesn’t Mean “Constant” in JavaScript

Noura MostafaNoura Mostafa
1 min read

Many beginners assume:

"const means the value can never change."

But that’s not exactly true.


✅ What const Really Means

  • const prevents reassignment of the variable identifier.

  • It doesn’t make the value itself immutable.

Example:

jsCopyEditconst user = { name: "Nora" };
user.name = "Lina"; // ✅ This works!

But:

jsCopyEdituser = { name: "Ali" }; // ❌ Error: Assignment to constant variable

🔐 If You Want to Freeze the Value

Use Object.freeze():

jsCopyEditconst settings = Object.freeze({ darkMode: true });
settings.darkMode = false; // ❌ No effect

Note: Object.freeze() is shallow — it won’t freeze nested objects.


🧠 Quick Recap

KeywordReassign?Mutable?
var
let
const✅ (unless frozen)

💡 Pro Tip: Use const by default. Only use let when reassignment is needed.

0
Subscribe to my newsletter

Read articles from Noura Mostafa directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Noura Mostafa
Noura Mostafa

🚀 Aspiring Full-Stack Developer Blogger 👨‍💻 Passionate about web development and coding. ✍️ Sharing my journey through tech via CodeOdyssey 🌍 "The code is a journey, not a destination."