A variable is a name that points at a value, so you can use that value again later without retyping it. Almost every line of JavaScript you’ll ever read either creates a variable or uses one, so it’s the natural place to start.
Modern JavaScript gives you three keywords for declaring a variable —
const, let, and var — and they are listed here in the order you’ll
actually run into them: const is the one you’ll write the most, let
shows up when a value needs to change, and var is a holdover from older
code that you’ll mostly only read, not write.
const — the one to reach for firstconst declares a variable whose binding can’t be reassigned. Once you
say const name = value, name will always point at that same value for the
rest of its scope.
const greeting = "Hello, world!";
const year = 2024;
const user = { name: "Ada", role: "admin" };
Breaking down the syntax:
const — the keyword that starts the declaration.greeting — the variable’s name (also called an identifier). Names
can contain letters, digits, $, and _, but can’t start with a digit.= — the assignment operator. It stores the value on the right inside
the name on the left."Hello, world!" — the value being stored.; — ends the statement.You can read a const as many times as you like:
const price = 19.99;
const tax = price * 0.07;
console.log(price + tax); // 21.3893
Why start here? Because most values in a program never need to change once
they’re created — a configuration object, a calculated total, a piece of text
you’re about to display. Declaring those with const tells anyone reading
the code (including future you) “this value is locked in,” which makes the
code easier to follow and prevents accidental reassignment bugs.
constonly locks the binding, not the value itself. An object or array stored in aconstcan still have its contents changed — you just can’t point the variable at a brand new object or array. The Objects and Arrays lessons later in this series cover that in detail.
let — for values that need to changeSometimes a value genuinely needs to be reassigned as your program runs — a
running total, a counter, the currently selected tab. That’s what let is
for.
let score = 0;
score = score + 10;
score += 5; // shorthand for score = score + 5
console.log(score); // 15
The syntax mirrors const exactly — let name = value; — except let
allows the name to be assigned a new value later with name = newValue.
You can also declare a let without an initial value, which starts it out as
undefined:
let total;
console.log(total); // undefined
total = 100;
A good rule of thumb: start every variable as const. If you later find you
need to reassign it, change it to let. That way let in your code is
always a signal that says “watch this one — it changes.”
var — the keyword you’ll mostly read, not writevar is the original way to declare a variable in JavaScript, and you’ll
still see it in older tutorials, libraries, and codebases.
var name = "Grace";
var count = 1;
count = count + 1;
On the surface var looks just like let — you can declare it, assign it,
and reassign it. The difference is in scoping, which is the subject of the
next section.
Before const and let arrived in 2015 (in a version of JavaScript called
ES2015, or “ES6”), var was the only way to declare a variable, so all
older JavaScript uses it.
var has two behaviors that regularly surprised people and led to bugs:
1. It ignores block scope. A variable declared with let or const
inside a { } block — like the body of an if statement or a loop — only
exists inside that block. A var leaks out of the block entirely:
if (true) {
let blockScoped = "only visible in here";
var leaks = "visible outside the block too";
}
console.log(leaks); // "visible outside the block too"
console.log(blockScoped); // ReferenceError: blockScoped is not defined
2. It “hoists” in a confusing way. JavaScript moves var declarations to
the top of their function before running any code, but not the value
assigned to them. That means you can reference a var before the line that
declares it and get undefined instead of an error — which usually means a
typo or logic mistake goes unnoticed:
console.log(mystery); // undefined (not an error!)
var mystery = "where did I come from?";
let and const fix both problems: they’re confined to the block they’re
declared in, and trying to use one before its declaration throws a clear
error instead of silently giving you undefined. That’s why virtually all
JavaScript written today reaches for const and let, and treats var as
something you only need to recognize when reading legacy code.