When should use const and let instead of var in Javascript?

Everyone talking about ECMAScript 6 big changes and variable declartion in ECMAScript 6(ES6). In terms of definations, here are the description about const, let and var:

`const` is a signal that the identifier won’t be reassigned.

`let`, is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.

`var` is now the weakest signal available when you define a variable in JavaScript. The variable may or may not be reassigned, and the variable may or may not be used for an entire function, or just for the purpose of a block or loop.

Let’s see an example to understand in a better way:

‘var’:

function run() {
console.log(a);
var a = 1;
}
run();

This will print `undefined` when `run` is invoked. Because, all the variables declared, with `var`, anywhere within the current function are hoisted. It means that the `var` declarations are processed during the compile time and the JavaScript engine will know that there is a variable with the name `a` exists somewhere within the function, but the value will be assigned only when then assignment expression is executed. Till then it will have the default value `undefined`. That is what happens in this case.

`let`:

‘use strict’;
function run() {
console.log(a);
let a = 1;
}
run();

You can’t use ‘let’ without ‘use strict’;, it throws error. Now, the code will throw a `ReferenceError`, because `let` makes sure that the variable `a` is visible only after decalaring a. there is one other scenario also:

‘use strict’;
function run() {
if (1 !== 1) {
let a = 1;
}
console.log(a);
}
run();

Again, the code will throw a `ReferenceError`, because `let` makes sure that the variable `a` is visible only after decalaring a to the `if` block. So, the code outside the block will not know about the variable `a`.

`const`: `const` is similar to C or C++’s `const` only. Once you assign a value to the variable declared as `const`, you cannot assign some other value to the const variable. For example,

‘use strict’;
const a = 1;
a = 2;

will throw `TypeError: Assignment to constant variable.`

Note: Don’t confuse `const` with mutable objects. As I said, you cannot assign some other value to the const variables, but you can modify the `const` value. Constant Reference, Not Value:The other new keyword for variable declaration in ES6 is const, but it is often misinterpreted as being a “constant value”. Instead, in ES6 a const represents a constant reference to a value (the same is true in most languages, in fact). In other words, the pointer that the variable name is using cannot change in memory, but the thing the variable points to might change. For example,

‘use strict’;
const a = {};
a.a = 1;

is perfectly valid, because we are not changing what is assigned to `a`, but we are changing the object pointed by `a`.

You can reach out to me for any doubt and suggestions. Please share this with others as well.

Oh, and if you like this, click the 💚 below. Keep watching this space and follow me for more articles on technology.

Thanks!
Happy Coding!!