What is the difference between var, let and const?
| Point | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Reassign | Yes | Yes | No |
| Redeclare | Yes | No | No |
| Hoisting | Hoisted, value undefined | Hoisted, but unusable before declaration | Same 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 |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |