let

The let and const keywords were introduced in ES6. In order to discuss what these are and how they work, let's review what the var keyword does. Prior to ES6, the way in which you initialized a variable was to use the var keyword. The two things to remember about var are the following:

  • When you use var to define a variable outside of a function body, it becomes globally scoped. This is to say that all the other functions in your JavaScript file have access to it. While this may sometimes be convenient, it can also be dangerous because the value may inadvertently be changed by a function other than the one that you intended the variable to be used for. This is possible when more than one function refers to the same variable name.
  • When you use var to define a variable within a function, it becomes locally scoped. In contrast to globally scoped variables, locally scoped variables are only accessible within the function in which they were created. This is true regardless of block scope because JavaScript variables declared with the var keyword are scoped to the nearest parent function.

You can, of course, still use var to declare and define your variables, since the keyword has not been deprecated. It's just that you now have more explicit control over the behavior of your initialization code, and the readability of the code has improved with let and const, since the intentions are clear for when you're looking at JavaScript code. 

The let keyword creates block scope local variables and gets its name from other languages that have a similar construct, such as Lisp, Clojure, Scala, and F#. In these languages, variables declared using let can be assigned, but not changed. However, in ES6, variables assigned using let can be changed; even so, regardless of whether they are changed or not, the variable is a local block scoped variable.

If you find this is a little confusing, you're not alone. Getting a firm understanding of the nuances in variable scoping is not something that you can learn by just reading. Programming is like learning math (or like learning most things, for that matter): the more you do it, the better you get. That being said, one way to boil it all down in your mind is to look at this one main difference between var and let: since you can have more than one block within a function, and even nested blocks (or sub-blocks), variables defined with the let keyword are accessible only within the block they are defined, as well as from that block's nested blocks. In contrast, the scope of variables defined with var is the entire closing function. Remember that one main difference, and you're golden.

Let's look at some code to understand the impact of the let keyword, and then we can move on to discussing the const keyword:

let x = 5;
if (x === 5) {
let x = 10;
console.log(x); // this line will output 10 to your console
// note, x was allowed to be changed
}
console.log(x);
// this line will output 5 to your console
// because the change to x was made within a block
..................Content has been hidden....................

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