← JavaScript Interview Questions

What is the difference between var, let, and const?

2
1
Asked 5 years ago by Hayk Hovhannisyan

Answers

ECMAScript Version:

let and const are introduced in ES2015 (ES6) before that only var was available.

Scope:

The variables declared by var are function scoped.

The variables declared by let and const are block scoped.

Hoisting:

All types of variables will be defined in their entire scope, variables defined by var can be accesses before the line where there are declared, let and const will throw an error in that case.

console.log(a); // undefined
var a = 1;
console.log(b); // ReferenceError
let b = 1;

1
Answered 5 years ago by Hayk Hovhannisyan