var, let & const in JavaScript
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.
const by default
const name = value; for anything assigned once — the binding can never point elsewhere afterward.
let when it changes
let count = 0; then count++ — block-scoped like const, but reassignable.
var is legacy
Function-scoped and hoisted with silent undefined — read it in old code, don't write it in new code.
const contents still mutate
const arr = []; arr.push(1) is legal — const locks the reference, not the object it points at.
const name = "Ada";let score = 0;score += 10;console.log(name, score);Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.