HuntExams Academy logo
JavaScriptBeginner#scope#es6

Difference between var, let and const?

Answer

var is function scoped and hoisted as undefined. let and const are block scoped and stay in the temporal dead zone until initialised. const cannot be reassigned, though object contents can still change.

Example

if (true) { var a = 1; let b = 2; }
console.log(a); // 1
console.log(b); // ReferenceError
const o = { n: 1 };
o.n = 2;  // allowed
o = {};   // TypeError

Related Interview Questions

1
JavaScriptBeginner#scope

What is hoisting?

Open
2
JavaScriptIntermediate#closures#functions

What is a closure?

Open
3
JavaScriptBeginner#operators#coercion

Explain == vs ===.

Open