← JavaScript tutorial

JavaScript

Variables (let, const, var)

let declares a variable you can reassign later. const declares one that can't be reassigned after its first value. var is an older way to declare variables with looser, more error-prone rules — modern JavaScript avoids it almost entirely.

Example: const by default, let only when needed

const name = "Ada";     // never reassigned — use const
let age = 21;            // will change below — needs let
age = age + 1;

document.getElementById("output").textContent = name + " is now " + age;

Reach for const by default, and switch to let only once you know a value genuinely needs to change later. This isn't just a style preference — a const that someone later tries to reassign throws an immediate error, catching a whole class of accidental-reassignment bugs the moment they happen, rather than silently letting a value change somewhere unexpected.

Try it yourself