Skip to main content

Difference between var, let, const

In JavaScript, var, let, and const are all used to declare variables, but they differ in their scope, hoisting, and reassignment capabilities. There are key differences:

Difference in scope

  • var is function or globally scoped,
  • let and const are block-scoped

Difference in redeclaration and reassignment

  • var allows redeclaration and reassignment.
  • let allows reassignment but not redeclaration in the same scope.
  • const does not allow reassignment or redeclaration.

Difference in hoisting

  • Variables declared with var are hoisted and can be accessed before their declaration (with undefined value)
  • Variables declared with let and const are also hoisted to the top of their block, but they should not be initialized. Accessing them before the declaration results in a ReferenceError (Temporal Dead Zone).