Video summary
Differences Between Var, Let, and Const
Main summary
Key takeaways
Main ideas / lessons
varwas the original JavaScript way to declare variables, but it can cause mistakes due to function scoping and other surprising behaviors.letandconstwere added to use block scoping instead of function scoping, which is generally safer and more predictable.- Key practical differences include:
- Scope (function vs block)
- Redeclaration/reassignment rules
- Temporal behavior (using before declaration)
- Immutability of the binding for
const(you can’t reassign, though object properties can still be mutated)
Conceptual comparison: var vs let vs const
1) Scope: function-scoped (var) vs block-scoped (let/const)
-
var- Function scoped
- Accessible anywhere within the declaring function, even outside an
ifblock / loop where it’s declared. - Effect shown: a
vardeclared inside anifblock can still be logged outside thatifblock without a scope error.
-
let- Block scoped
- Accessible only inside the block it’s declared in (e.g., only within an
ifstatement or a loop). - Effect shown: logging a
letvariable outside theifblock causes an error because it is not found.
-
const- Behaves like
letregarding scoping: also block scoped.
- Behaves like
2) Redeclaration / reassignment behavior
-
var- Allows redeclaration and reassignment (the example shows re-declaring
varcan overwrite the earlier value). - Lesson: this increases the risk of accidentally overwriting variables.
- Allows redeclaration and reassignment (the example shows re-declaring
-
let- Does not allow redeclaration in the same scope.
- Lesson: prevents common mistakes from overwriting variables unintentionally.
-
const- Like
letfor preventing redeclaration/reassignment of the binding. - Lesson: you cannot assign a new value to a
constvariable.
- Like
3) Using a variable before it is declared
-
var- Allows code to reference the variable before its declaration.
- Behavior: instead of a “not defined” error, the value behaves as
undefineduntil the declaration is evaluated.
-
let- Referencing before declaration results in an error (the variable is not available outside its declared block/temporal rules).
4) const immutability: binding vs object contents
-
constvariables cannot be reassigned:- If you try to assign a different primitive value later, you get an error (example:
assignment to constant variable is not allowed).
- If you try to assign a different primitive value later, you get an error (example:
-
But
constallows mutating object properties:- If
constholds an object, you can change its properties without reassigning the variable itself. - Lesson:
constprevents rebinding (changing what the variable points to), not necessarily mutation of nested properties.
- If
Recommended usage rules
- Use
constwhen the variable’s value should not change. - Use
letwhen the variable must change (example: loop iterator). - Default rule:
- Prefer
constandletin almost all cases overvar.
- Prefer
- Avoid
varunless there is a specific, justified case.- Rationale:
varhas wider (function-level) scope and behaviors that can lead to accidental bugs (overwrites, availability before declaration asundefined, etc.).
- Rationale:
Speakers / sources featured
- Kyle (the video narrator/author)