🚀 JavaScript Fundamentals

Wael HajjiWael Hajji
3 min read

Chapter 1: Variable Declaration & Scoped Memory Patterns

👋 Welcome to the first stop on our JavaScript journey!

We’re not going to drown in walls of code or memorize syntax like robots. Instead, we’ll explore why things work the way they do, how experts think about it, and the little quirks that make JavaScript both beautiful and (sometimes) mischievous.

So grab your coffee ☕ (or tea 🍵), and let’s dive into the world of variables — the building blocks of every program.


🌱 The Evolution of Variables in JS

Before ES6 (2015), JavaScript only had var. Developers had to deal with its… let’s say “quirky personality.”

Then came let and const, giving developers modern tools that made our code safer, cleaner, and less error-prone.

Think of them as three types of storage boxes:

  • 🗄️ var → the old, leaky storage box. It does the job, but sometimes spills out where you don’t want it (global scope).

  • 📦 let → the modern, secure box. It keeps things tidy and contained (block scope).

  • 🔒 const → the unmovable box. Once placed, it can’t be reassigned — but you can still rearrange the items inside (objects & arrays).


🧠 Key Concepts You Must Know

  1. Scope Rules

    • var → function-scoped (or global if declared outside).

    • let & const → block-scoped (exists only inside { }).

  2. Hoisting & TDZ (Temporal Dead Zone)

    • Variables declared with var are hoisted — JS pretends they exist before their declaration.

    • let and const are also hoisted, but locked in the TDZ until the line where they’re defined.

  3. Const ≠ Immutable

    • Declaring an object or array with const doesn’t freeze it.

    • It only prevents reassignment of the reference, not changes to the contents.


💻 Code Examples Explained

// Example 1: var is leaky
if (true) {
  var oldBox = "I escape the block!";
}
console.log(oldBox); // ✅ Prints: "I escape the block!"
// Surprise! The variable leaked out of the block
// Example 2: let is safer
if (true) {
  let safeBox = "I stay where I belong!";
}
console.log(safeBox); // ❌ ReferenceError
// `safeBox` only exists inside the if-block
// Example 3: const with objects
const hero = { name: "Iron Man" };
hero.name = "Tony Stark"; // ✅ Allowed (mutating object)
hero = {};                // ❌ Error (cannot reassign)
// Const protects the *reference*, not the content

🌍 Real-World Analogy

Imagine renting apartments:

  • 🏚️ var → You rent an old apartment with a broken lock. Anyone can walk in.

  • 🏠 let → You rent a modern apartment with strong security. Only you can access it.

  • 🏢 const → You own the apartment itself. You can remodel the inside, but you can’t suddenly move the whole building somewhere else.


📖 Expert Perspective

“Always declare variables with const by default. Switch to let only if reassignment is needed. Avoid var entirely unless you’re debugging legacy code.”
Kyle Simpson, Author of “You Don’t Know JS”


🎯 Quick Recap

  • ✅ Use let for variables that change.

  • ✅ Use const for variables that don’t change (or shouldn’t be reassigned).

  • 🚫 Avoid var in modern code.

👉 These habits make your code cleaner, easier to debug, and closer to how professional developers write JavaScript today.


🔮 Coming Up Next

Next, we’ll uncover the strange world of data types & type coercion — where numbers, strings, and booleans play tricks on each other.

Stay tuned… because JavaScript loves surprises. 😉


✨ And that’s it for Chapter 1!

10
Subscribe to my newsletter

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

Written by

Wael Hajji
Wael Hajji

From starting with nothing to mentoring 250+ developers, I’m passionate about turning complex code into clear, confident skills. As a Full-Stack Instructor and Instructional Support Manager, I believe in the power of storytelling to make learning enjoyable and impactful. Join me on a journey where code meets creativity, growth, and real-world success.