What is the difference between var, let and const?

Pointvarletconst
ScopeFunctionBlockBlock
ReassignYesYesNo
RedeclareYesNoNo
HoistingHoisted, value undefinedHoisted, but unusable before declarationSame as let
if (true) {
  var a = 1;
  let b = 2;
}
console.log(a);
console.log(b);

Output:

Tutorials Class - Output Window

1
ReferenceError: b is not defined

Use const by default, let if the value must change, and avoid var. See JavaScript Variables and Scope.


Learn more about the similar topics:
Tutorials
JavaScript Variables
JavaScript Scope
JavaScript Data Types
JavaScript Type Conversion
JavaScript Operators
JavaScript Numbers and Math
Exercises & Assignments
Write a JavaScript program to add two numbers
Write a JavaScript program to swap two variables
Interview Questions & Answers
What are the data types in JavaScript?
What is the difference between == and === in JavaScript?
What is NaN in JavaScript?
What is the difference between null and undefined?
What is scope in JavaScript?
What is the difference between var, let and const?
What is hoisting in JavaScript?