②var js difference in the definition of the variables of let const

var have talked about in the previous article, it is the key word js-defined variables.

But in es6, the two newly added keyword, keyword for variable declaration: let and const

Then say about the difference between const and var let's:

First of all that var

Var with the definition of variables, you can re-assignment, when I assigned in a function, after calling the function, while changing the values ​​of local variables.

 

Next, let say

Not repeat declare a variable with let.

When I re-assignment to the same variable in a function and will not affect variables outside of my function. let is block-level scope.

 

Finally, const

const define the variable assignment can not be repeated. And, const, and let all have the same block-level scope.

 

What and when to use const let it?

When we define a variable that does not change with the const

Other times and all of them with a let, eg: for loop

Guess you like

Origin www.cnblogs.com/JiAyInNnNn/p/10993054.html