Variables: let, const, and var

A variable is a labeled box where your program stores a piece of information so it can use it later.

What Is a Variable?

A variable is a name you give to a piece of data so you can refer back to it later instead of retyping it. In JavaScript you create one using the keyword let, const, or the older var.

let age = 18;
  • let — a keyword meaning "I'm creating a new variable that can change later"
  • age — the name we chose for this box
  • = — the assignment operator; it means "put this value into that box," not "equals" like in math
  • 18 — the value being stored
  • ; — ends the instruction

Behind the scenes, the computer sets aside a small space in memory, labels that space age, and stores the number 18 there. Whenever you write age again, JavaScript goes and fetches whatever is currently in that box.

📦 Real-life analogy

A variable is like a labeled storage box. You write "age" on a sticky note and put it on a box, then place the number 18 inside. Later, you can look at the box labeled "age" to remember what's inside — or open it up and swap in a new value.

Why Do We Use Variables?

Without variables, every piece of data you use would need to be typed out, every single time, with no way to update it. Variables let you:

  • Store a value once and reuse it many times
  • Give data a meaningful name (userAge is clearer than a bare 18)
  • Update a value as your program runs (like a score that goes up)

let vs const vs var

KeywordCan it change later?Should you use it?
constNo — value is locked inYes, use by default
letYes — value can be reassignedYes, when the value needs to change
varYes, but with confusing old rulesNo — avoid in modern code

const — "this never changes"

const birthYear = 2008;
// birthYear = 2009;   ❌ This would cause an error — const cannot be reassigned

let — "this can change"

let score = 0;
score = 10;   // ✅ totally fine, let allows this

var — the old way

var name = "Sam";

var was the only option before 2015. It still works, but it behaves in surprising ways around scope (covered in the Scope & Hoisting lesson) — modern JavaScript avoids it.

When Should We Use Each One?

  • Start with const for everything.
  • Switch to let only if you know the value must change later (a counter, a running total, a "current page" tracker).
  • Don't use var in new code — it's kept in the language only so old websites keep working.

Naming Variables

  • Names can contain letters, numbers, $, and _, but can't start with a number.
  • JavaScript is case-sensitive: age and Age are different variables.
  • The convention is camelCase: firstName, totalPrice, isLoggedIn.
  • Names should describe what the value is, not how it's used.

Common Mistakes

⚠ Watch out for
  • Trying to reassign a const — this throws TypeError: Assignment to constant variable.
  • Using a variable before declaring it — this behaves differently for let/const (error) versus var (silently undefined). More in the Hoisting lesson.
  • Declaring the same variable twice with let in the same scope — JavaScript will refuse.

Best Practices

  • Default to const; it protects you from accidentally overwriting a value.
  • Use clear, descriptive names instead of single letters (except in short loops).
  • Declare one variable per line for readability.

Performance Notes

let and const are not meaningfully slower or faster than var in modern engines — the reason to prefer them is safety and clarity, not speed.

Browser Compatibility

let and const have been supported in all major browsers since 2015 (ES6). var works everywhere, including very old browsers.

Interview Questions

What's the difference between let and const?

Both are block-scoped, but a variable declared with const cannot be reassigned after its initial value is set, while let can be reassigned as many times as needed.

Can you change the contents of a const object or array?

Yes. const only locks the variable itself from being reassigned to a new value — it doesn't freeze the contents. You can still change properties inside a const object or push items into a const array.

✏️ Exercise

Create a const called myName with your name, and a let called mood set to how you're feeling. Then change mood to something else and log both.

🧠 Quiz

1. Which keyword should you reach for by default?

const.

2. What happens if you try to reassign a const variable?

JavaScript throws a TypeError and stops execution.

🚀 Mini Challenge

Write a small "bank balance" simulation: start with let balance = 100, then subtract 20, add 50, and log the balance after each change.

Summary

  • Variables are named storage for data.
  • Use const by default, let when the value changes, and avoid var.
  • Variable names are case-sensitive and use camelCase by convention.

Related Topics

MDN reference: developer.mozilla.org → JavaScript → let, const (placeholder — add live link)