Variables (var, let and const) in javascript
All these three keywords are used to define a variable in javascript. Here are differences between them in terms of scope, redeclaration, reassignment and hoisting.
var​
- functional or global scoped
- can be redeclared
- can be reassigned
- hoisting —> can be able to access the variable before defining it. It is called hoisting.
let​
- blocked scoped
- cannot be redeclared
- can be reassigned
- cannot access the variable before declaration ( hoisting is not supported)
const​
- blocked scoped
- cannot be redeclared
- cannot be reassigned
- cannot access the variable before declaration ( hoisting is not supported)