Learn JavaScript
❯ variables.var_let_const

var, let & const in JavaScript

$concept

Three declarations, three eras: var (function-scoped, hoisted, redeclareable — legacy), let (block-scoped, reassignable — the default variable), const (block-scoped, single-assignment — the default overall). Modern code uses const unless reassignment is needed, then let — var survives only in old codebases.

const's subtlety: the binding is fixed, not the value — const arr = [] forbids arr = other but happily allows arr.push(x). Immutability of contents needs freezing or discipline, not const alone.

$how it works
1

const by default

const name = value; for anything assigned once — the binding can never point elsewhere afterward.

2

let when it changes

let count = 0; then count++ — block-scoped like const, but reassignable.

3

var is legacy

Function-scoped and hoisted with silent undefined — read it in old code, don't write it in new code.

4

const contents still mutate

const arr = []; arr.push(1) is legal — const locks the reference, not the object it points at.

$example
const name = "Ada";
let score = 0;
score += 10;
console.log(name, score);
Output
Ada 10
1name binds once via const — reassignment would throw.
2score starts 0 via let, then += 10 reassigns legally to 10.
3Both print — const for the fixed, let for the changing.
$use cases
Defaulting to const and downgrading to let only on reassignment
Reading var correctly in legacy code (function scope, hoisted undefined)
Mutating const arrays/objects deliberately while locking the binding
Banning new var with lint rules (no-var)

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.