This content originally appeared on DEV Community and was authored by wael.hajji
Chapter 1: Variable Declaration & Scoped Memory Patterns
โIf you donโt understand variables, you donโt understand programming.โ โ Every developer ever.
Welcome, fellow coder!
This is not just another corner of the internet where I paste code and call it a day. Nope.
Here, Iโll walk you through the story behind concepts, the โaha!โ moments, the things I wish someone explained better when I started.
So buckle up โ weโre kicking things off with variables in JavaScript.
Whatโs the Big Deal with Variables?
Imagine your brain as a giant office full of drawers.
Each drawer has a label (the variable name) and stuff inside it (the value).
JavaScript gives us three types of drawers to work with:
- var โ The messy old drawer everyone used before. Always open, stuff falls out.
- let โ A neat drawer you can open only in the right room (block scope).
- const โ A locked drawer. Once you label it, the label never changes โ but the contents might still shuffle.
Scope = Where Your Drawers Exist
Scope is like the room your drawer lives in.
Global scope: Everyone in the building sees it.
Function scope (var
): Only people inside this office can see it.
Block scope (let
/const
): Only people in this little corner see it.
Example:
if (true) {
var oldDrawer = "I escape!";
let neatDrawer = "I stay here!";
const lockedDrawer = "Iโm also here!";
}
console.log(oldDrawer); // ✅ Works
console.log(neatDrawer); // ❌ Error
console.log(lockedDrawer); // ❌ Error
See the difference? var leaks, while let/const stay local.
Hoisting โ JavaScriptโs Magic Trick
JavaScript doesnโt actually run top-to-bottom like youโd think.
It secretly lifts (hoists) declarations to the top before executing.
console.log(x); // undefined, but no error
var x = 5;
But with let/const, you get the mysterious Temporal Dead Zone (TDZ):
console.log(y); // ❌ ReferenceError
let y = 10;`
Think of it as JavaScript saying:
โHey, I know about y
โฆ but you canโt touch it yet.โ
Why const Is More Popular Than You Think
A lot of devs use const even for objects and arrays.
Why? Because in JavaScript, const doesnโt mean immutable. It just means you canโt reassign the variable name.
const car = { brand: "Tesla" };
car.brand = "BMW"; // ✅ Works
car = { brand: "Audi" }; // ❌ Error
This teaches us a crucial point:
Variables store references, not the actual data.
Real-World Analogy
Think of let
and const
as renting an apartment in a gated community.
You control who has the keys (scope), but once the contract (const) is signed, you canโt just move to another apartment. You can, however, redecorate the inside.
Learning Goals Recap
By now, you should:
Know the difference between
var
, let
, and const
Understand scope & the TDZ
Predict hoisting behavior
Appreciate why
const
is safer for predictable code
Discussion Prompt
Why might a developer opt for const
even when dealing with non-constant objects?
What does that reveal about how JavaScript handles references?
Final Words
Variables arenโt just a โfirst chapterโ thing.
Theyโre the foundation youโll lean on all the way to React, Node, and** MERN**.
Master this, and youโve just sharpened one of the most important tools in your developer toolbox.
Letโs build. Letโs laugh. Letโs level up.
Welcome to my journey โ and maybe yours too.
This content originally appeared on DEV Community and was authored by wael.hajji