Constant Variables (But Not as Constant as You Think 👀)
When you start writing JavaScript, you quickly meet a trio:
var→ almost retired, but still hanging aroundlet→ modern, logical, reliableconst→ once it gives its word, it never backs down 😎
In this article, the spotlight is on const.
But here’s a small spoiler: const does not always mean “absolutely unchangeable.”
Yes, JavaScript is once again inviting us to a little mental workout.
What Is const? What Does It Do? What Doesn’t It Do?
const is used to define variables that cannot be reassigned.
const siteName = "CodeDünyası";
With this line, you’re basically telling JavaScript:
“Hey buddy, this variable is called
siteName.
This is its value.
Don’t try to overwrite it.”
Now let’s do this:
siteName = "AnotherSite";
JavaScript reacts immediately:
❌ TypeError: Assignment to constant variable
Which, translated from JS language, means:
“I’m not allowing this. We had an agreement.”
Summary: const = no reassignment ❌
The Golden Rule of Using const 🏆
A const must receive a value at the moment it is declared.
Wrong usage (JavaScript gets angry):
const age;
age = 25;
Correct usage:
const age = 25;
JavaScript is very clear here:
- “I’ll assign it later” ❌
- “Let me think about it” ❌
- “I’m not sure yet” ❌
If you declare it, you assign it. End of story.
Why Does Everyone Use const?
Because const:
- Communicates intent in your code
- Eliminates the risk of accidental reassignment
- Tells your teammates (and your future self): “This value won’t change, don’t waste time touching it.”
In modern JavaScript, there’s an unwritten but widely accepted rule:
Use
constfirst, switch toletonly if necessary
So your default reflex should be:
const total = 100;
If it really needs to change:
let total = 100;
total = 120;
Plot Twist 🎬 const + Objects
Now we’re getting to the fun part.
const user = {
name: "Ahmet",
age: 30
};
This variable is declared with const. So what happens next?
1️⃣ Modifying the object’s contents
user.age = 31;
✅ WORKS
Why?
constlocks the reference- The inside of the object is a free playground 🎈
JavaScript is basically saying:
“You can keep using the same object, just feel free to rearrange what’s inside.”
2️⃣ Replacing the entire object
user = { name: "Mehmet", age: 25 };
❌ ERROR
Because this is a brand-new reference, and const does not allow that.
Same Story: const + Arrays 🍿
const numbers = [1, 2, 3];
Let’s modify the array:
numbers.push(4);
✅ Works
numbers[0] = 99;
✅ Works
But if you do this:
numbers = [5, 6, 7];
❌ Error
In short:
- The content can change
- The reference cannot
If You Want const to Be Really Constant 🧊
If you want to prevent even internal changes:
const settings = Object.freeze({
theme: "dark",
language: "tr"
});
settings.theme = "light";
❌ No change (throws an error in strict mode)
💡 Tip: Object.freeze is shallow. Nested objects need extra precautions.
Where Is const Used in Real Life?
🔗 API URLs
const API_URL = "https://api.example.com";
🧮 Functions
const calculateTotal = (price, tax) => price + tax;
🖱️ DOM elements
const submitButton = document.querySelector("#submitBtn");
⚙️ Configuration values
const MAX_RETRY_COUNT = 3;
If these values aren’t supposed to change, const is your best friend ❤️
Small Mistakes, Big Lessons 🚧
❌ Using let everywhere
Unnecessary risk
❌ Misunderstanding const
“Why did this change?” panic
✅ Conscious use of const
Clean, safe, readable code
Burn It Into Your Brain 🧠 (Ultra Summary)
constprevents reassignment- It must be initialized immediately
- Object and array contents can change
- References cannot change
- It’s the default choice in modern JavaScript
Closing 🎤
In JavaScript, const:
- Communicates intent
- Reduces bugs
- Protects you from yourself 😄
If you still reflexively write let everywhere…
somewhere out there, a const is quietly feeling sad.
