Difference between JavaScript variable keyword (var, let, const)
1 min readMay 5, 2021
Before ECMAScript 2015, JavaScript had only one variable keyword called var. ECMAScript 2015 introduces us to two new variable keywords known as let and const.
const: If any variables are defined with const, they cannot be reassigned.
Example:
const fruit = ‘Mango’;fruit = ‘Apple’; // You cannot change the value, which is defined with const, so it will throw an error.
let: it’s a block scoped {}. So, if the variables are defined with let, it will only work within a block {}.
// num will not work here, because it is outside the for loop blockfor (let num = 0; num < 5; num++) {// num variable is only work in here}// num will not work here, because it is outside the for loop block
var: if the variables are defined with var, they will work inside and outside a block {}.
// num will work in herefor (let num = 0; num < 5; num++) {// num will work in here}// num will work in here