Skip to content
Happy Programming Guide
Start learning
JavaScript

JavaScript Variables: let, const and var Explained

Which keyword to use and why. Scope, hoisting, reassignment versus mutation, and why const objects can still be changed.

JavaScript has three ways to declare a variable. Two of them you should use, one you should not.

JavaScript
const name = "Ada";    // use this by default
let score = 0;         // use this when the value must change
var old = "avoid";     // legacy — do not use in new code

const: cannot be reassigned#

JavaScript
const rate = 12;
rate = 15;    // TypeError: Assignment to constant variable.

Start with const. If you later find you genuinely need to reassign, change it to let. This habit makes code easier to follow, because every const is a promise that the name will keep meaning the same thing.

The important subtlety#

const stops you reassigning the name. It does not freeze the contents.

JavaScript
const scores = [10, 8];
scores.push(9);        // fine — the array changed
console.log(scores);   // [10, 8, 9]

scores = [1, 2];       // TypeError — the name cannot point somewhere new

Same for objects: person.age = 37 is allowed on a const person.

let: reassignable#

JavaScript
let total = 0;

for (const price of [100, 250, 80]) {
  total += price;
}

console.log(total);   // 430

Block scope#

let and const exist only inside the nearest pair of braces.

JavaScript
if (true) {
  const secret = "hidden";
  console.log(secret);   // works
}

console.log(secret);     // ReferenceError: secret is not defined

This is why var is avoided. var leaks out of blocks, which produces bugs that are painful to trace:

JavaScript
if (true) {
  var leaked = "still here";
}
console.log(leaked);     // "still here" — surprising and unhelpful

Naming#

  • Start with a letter, _ or $; never a digit
  • No spaces or dashes
  • Case sensitive: userName and username differ
  • Convention is camelCase for variables and functions, PascalCase for classes and React components, UPPER_SNAKE for fixed configuration values

undefined versus null#

JavaScript
let a;
console.log(a);          // undefined — declared but never given a value

const b = null;
console.log(b);          // null — deliberately empty

Roughly: undefined means “nobody set this”, null means “someone set this to nothing on purpose”.

Two shortcuts worth knowing#

JavaScript
const name = user.name ?? "Guest";       // fallback only if null/undefined
const city = user?.address?.city;        // no crash if address is missing

The second one prevents the very common “cannot read properties of undefined” error — see common JavaScript errors.

Questions people ask#

Should I ever use var?

Only when maintaining old code that already uses it. There is no reason to write new var declarations.

What if I forget the keyword entirely?

Writing score = 5 with no keyword creates a global variable, which is almost never what you want. Adding "use strict"; at the top of a file turns that into an error instead.

Is const faster?

No meaningful difference. Use it for clarity, not speed.

Where to go next#

Next lessonJavaScript functions and arrow syntax

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *