ES6 -Let And Const

In this post, I’ll introduce let and const, two new keywords added to JavaScript with the arrival of ES6, this will enable to define block-scope variable in JS. before explaining about Let and Const, let’s understand why Var declaration has some scope issues called “temporal dead zone”. Up to ES5, JavaScript had only two types of scope, functional scope and global scope. variable declared with var have functional scope. They are accessible inside the function they are defined in.
How ‘Hoisting’ works in Javascript.


var a = 'Hello World!';
function b(){
    console.log('Called b!');
}
b();
console.log(a);

the output we expect as exactly like below.
Called b!
Hello Wolrd!
now move the last 2 statements to top as below


b();
console.log(a);
var a = 'Hello World!'; function b(){ console.log('Called b!'); }

Now when you run the program, you will see an output below
Called b!
undefined // check below figure to understand how JavaScript Hoisting works.

The variable var a, are initialized to ‘undefined’, to overcome this issue, we can use Let keyword.

Variables declared with let have block scope. They are valid inside the block they are defined in.

 


console.log(guessMe); //A. guessMe is undefined
{
    console.log(guessMe); // B:  Uncaught ReferenceError: guessMe is not defined
    let guessMe = 5;
    console.log(guessMe); // C: guessMe is 5
}
console.log(guessMe) //D: guessMe is undefined

 

Comment B is called temporal dead zone of guessMe by accessing it before the variable was defined. Unlike var, variables declared using let aren’t hoisted. If you reference a variable in a block before the let declaration for that variable is encountered, this results in a Reference Error.  Always be grateful for errors pointing out obvious mistakes during development, as the same mistake tend to be a lot more expensive once they are deployed to production.

Constants

Declarations with const are block scoped, they have to be initialized, and their value cannot be changed after initialization.const follow the same scope rules as variables, but they can’t be redeclared. constants also share a feature with variables declared using let in that they are block-scoped instead of function-scoped. In case you try to access a constant before it’s declared, you will receive a ReferenceError. If you try to assign a different value to a const variable , you will receive a TypeError.

const PI = 3.1415;

Note: As Mathias Bynes state in his blog post (ES2015 const is not about immutability) . const  creates an immutable binding, but does not indicate that a value is immutable.


const foo ={};
foo.bar = 42;
console.log(foo.bar);
// 42

If you want to make an object’s value truly immutable, use Object.freeze().

User cases of let, const, and var:

Rule 1: use let for variables and const for constants whenever possible. Use var only when you have to maintain legacy code.

Rule 2: Always declare and initialize all your variables at the beginning of your scope.

If you have a linter such as ESLint, set it up accordingly, so that it warns you when you violate the second rule.

 

 

 

local_offerevent_note April 24, 2019

account_box srinivasaraju nadimpalli


local_offer