Video summary

Differences Between Var, Let, and Const

Main summary

Key takeaways

Educational

Main ideas / lessons

  • var was the original JavaScript way to declare variables, but it can cause mistakes due to function scoping and other surprising behaviors.
  • let and const were 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 if block / loop where it’s declared.
    • Effect shown: a var declared inside an if block can still be logged outside that if block without a scope error.
  • let

    • Block scoped
    • Accessible only inside the block it’s declared in (e.g., only within an if statement or a loop).
    • Effect shown: logging a let variable outside the if block causes an error because it is not found.
  • const

    • Behaves like let regarding scoping: also block scoped.

2) Redeclaration / reassignment behavior

  • var

    • Allows redeclaration and reassignment (the example shows re-declaring var can overwrite the earlier value).
    • Lesson: this increases the risk of accidentally overwriting variables.
  • let

    • Does not allow redeclaration in the same scope.
    • Lesson: prevents common mistakes from overwriting variables unintentionally.
  • const

    • Like let for preventing redeclaration/reassignment of the binding.
    • Lesson: you cannot assign a new value to a const variable.

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 undefined until 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

  • const variables 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).
  • But const allows mutating object properties:

    • If const holds an object, you can change its properties without reassigning the variable itself.
    • Lesson: const prevents rebinding (changing what the variable points to), not necessarily mutation of nested properties.

Recommended usage rules

  • Use const when the variable’s value should not change.
  • Use let when the variable must change (example: loop iterator).
  • Default rule:
    • Prefer const and let in almost all cases over var.
  • Avoid var unless there is a specific, justified case.
    • Rationale: var has wider (function-level) scope and behaviors that can lead to accidental bugs (overwrites, availability before declaration as undefined, etc.).

Speakers / sources featured

  • Kyle (the video narrator/author)

Original video