1, the var

Before let and const, we could only define variables with var. However, there are many problems in defining variables with var, which is often called variable promotion. For example:

if (false) {
  var a = 1;
}
console.log(a);
Copy the code

If you’re a beginner looking at this code for the first time, you’re going to think that this code is going to report an error, because the condition in if is false, and you’re not executing var a = 1, so you’re going to end up with an error where a is undefined, but instead you end up with undefined, which is a variable lift. In the case of variable lifts, MDN says that the following: Variable lifts are considered an acknowledgement of the way execution context works in Javascript, particularly at the build and execute stages. In JavaScript documentation prior to ECMAScript® 2015 Language Specification, the word “variable upgrade” cannot be found. Be aware, however, that this concept can be difficult and even annoying at first. That’s gone? That’s it! I had the same question when I looked at it, so I found this sentence in the var description: variable declarations, no matter where they occur, are processed before any code is executed. So the above code can be translated like this:

var a;
if (false) {
  a = 1;
}
console.log(a);
Copy the code

2, let

Let’s first look at a few features of let:

1. No promotion declared

Let’s look at it in the code:

if (false) {
  let a = 1;
}
console.log(a);
Copy the code
The execution result

We’re ecstatic to finally get the result we wanted, error, a is not defined.

2, repeat statement error

var a = 1;
let a = 2;
Copy the code
The execution result

Wow, this error shows that a is already defined. This is so friendly that you won’t accidentally change the global variable and avoid the danger of repeated naming.

3. Do not bind to global scope

let a = 1;
console.log(window.a); // undefined
Copy the code

It is not defined globally at all, mainly because let is block-scoped, not global scoped.

3, const

In development, we usually define const as a constant, that is, an unchangeable quantity. Its basic properties are similar to let.

const a = 1;
a = 3;
Copy the code

When the above code is executed, an error will be reported because const defines a constant and cannot be modified. But when const defines a reference datatype:

const a = {
  value: 1.};
a.value = 2;
console.log(a); // {value: 2}
Copy the code

This is because const defines the address of the reference. It determines if the address of the reference has changed. If you change the data in the reference data, the address of the reference has not changed, so you can change it.

After the language

Related articles:

  • Take you relearn ES6 | var, let and the difference between const
  • Take you relearn ES6 | Promsie
  • Take you relearn ES6 | Generator
  • Take you relearn ES6 | Async and Await
  • Take you relearn ES6 | Set and Map
  • Take you relearn ES6 | Symbol (more than just a new data type)
  • Take you relearn ES6 | Exprort (keep in mind that the output is variable)
  • Take you relearn ES6 | proxy and defineProperty

I think it’s ok, please give me a thumbs up when I leave, and we can study and discuss together!

You can also follow my blog and hope to give me a Start on Github, you will find a problem, almost all my user names are related to tomatoes, because I really like tomatoes ❤️!!

The guy who wants to follow the car without getting lost also hopes to follow the public account or scan the qr code below 👇👇👇.

I am a primary school student in the field of programming, your encouragement is the motivation for me to keep moving forward, 😄 hope to refueling together.

This article was typeset using MDNICE