← 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
Ready to go further?

These free tutorials are a preview, our Frontend Development cohort covers it in depth, with a real project and a cohort alongside you. Closes Nov 30

Enroll in Frontend Development

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account