In with let

let is the sensible replacement for var. Anywhere we used var correctly before we can interchange it with let. let removes the issues that plague var and is less error prone.

No Redefinition

let does not permit a variable in a scope to be redefined. Unlike var, let behaves a lot like variable definitions in other languages that strictly enforce variable declarations and scope. If a variable is already defined, then using let to redefine that variable will result in an error, as in the next example.

 'use strict'​;
 //BROKEN_CODE
 let​ max = 100;
 console.log(max);
 
 let​ max = 200;
 console.log(max);

This example is identical to the one we saw earlier, except that var was replaced with let. The compiler gives an error that max can’t be redefined, as we see in the output:

 let max = 200;
  ^
 
 SyntaxError: Identifier 'max' has already been declared

let brings variable declaration semantics in JavaScript on par with what’s expected in general programming.

What if we define a variable using var and then try to redefine it using let or vice versa? First, we should avoid such immoral thoughts—no reason to use var anymore. Second, JavaScript will not permit redefining a variable when let is used in the original definition or in the redefinition.

The fact that let does not allow redefinition is mostly good. There is, however, one place where that may not be to our advantage—in the REPL. As we saw in Run Using the REPL, we can use node also as a quick experimentation tool. Likewise, as we saw in Run in the Browser Console, we may also use the browser console to experiment and try out different code. When experimenting, we’d want to write and quickly change code to try out different ideas. In a few languages that have REPL and also prohibit variable redefinition, the rules of redefinition are favorably relaxed in REPLs for developer convenience. Sadly, node and the console in some of the popular browsers enforce the rule of prohibiting redefinition, thus making it a bit hard to retype chunks of code with variable definitions even within the console or REPL.

Block Scope

Variables declared using let have block scope. Their use and visibility is limited to the block of code enclosed by the {...} in which they’re defined. Furthermore, unlike var, variables defined using let are available only after their point of definition. That is, the variables are not hoisted to the top of the function or the block in which they’re defined.

Let’s convert var to let in the code we saw earlier where we used a variable defined within a loop from outside the loop.

 'use strict'​;
 
 //console.log(message); //ERROR if this line is uncommented
 
 console.log(​'Entering loop'​);
 for​(​let​ i = 0; i < 3; i++) {
 //console.log(message); //ERROR if this line is uncommented
 let​ message = ​'spill '​ + i;
 }
 console.log(​'Exiting loop'​);
 
 //console.log(message); //ERROR if this line is uncommented

This code illustrates the semantic difference between var and let. First, the variable defined within the block is not visible outside the block. Furthermore, even within the block, the variable is not visible before the point of definition. That’s semantically sensible behavior—just the way it should be.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset