The difference between var, let, and const

Let's understand what is the difference between var, let, and const and when should we use each of them.

·

1 min read

Different programming languages use different ways to declare a variable. In JavaScript, we use keywords var, let, and const to do the same.

Each of these keywords behaves in a different way in terms of Scope, Hoisting, Reassigning the value, and Redeclaration of the variable. Let's see these aspects one by one.

Scope

Variables declared with var are in the function scope. Variables declared with let and const are in the block scope.

Hoisting

Hoisting is allowed with var and it is not allowed let and const.

Reassign the value

Reassign of the value in a variable is not allowed when it is declared with const but is allowed when the variable is declared with let and var.

Redeclaration of the variable

The variable can be redeclared only if it is declared with var.