Today, Bao with exhibition zhao wandering in the street, when suddenly I saw 4 women a =1 var a =1 const a =1 let a =1 came to the front of Bao adults, beg bao adults to make decisions, especially var, can get Bao adults confused, not immediately the court case, Declare JavaScript(es3 and ES6 are the same person, but at different times)

var

  • A is equal to 1, var a is equal to 1 is for es3

  • Let a = 1, const a = 1 is es6

Why is the ES6 so fickle? Found 2 again, is the money to change the heart or the former is not good? \

Es6 said: home that var woman niang, all not good, if good words I again why bother to find 😩\

Var: What are you talking about? \

Es6: LET me talk about you. Usually I swallow my pride and now I want to say your kindness in front of Bao

  • When a = 1, you are always declaring a global variable \ when var is absent

A =1: I just sometimes, where are you so exaggerated \

  • The following cases I’m just at home when I’m the global variable
    a = 1
    console.log(window.a)
Copy the code
  • But if I were somewhere else I wouldn’t necessarily be a global variable
   var a 
   function fn() {
       var a 
       function fn2() {
           a = 1
       }
   }
   
   fn()
Copy the code

When I had a two-layer function, A = 1 didn’t declare a global variable, I used the local variable A \

Bao: So that in the absence of this variable is a premise, a = 1, will be implicitly declared a global variable, if it has a variable a, if not put himself as a global variable, this meaning is the oil going, you really will be a little let people scratching their heads, but listen to your speaking is almost clear, But it’s easy to confuse things when they’re complicated. Var a = 1: My Lord, it is because a = 1, easy to confuse the prime minister, and then the prime minister just find me.

    function fn() {
       var a = 1
       console.log(a)
   }
Copy the code
  • You see my 1 is my A.

Es6: Yes, you are fine, but you will improve yourself. You like to get ahead of me in whatever I do

 / / an error
  function fn() {
       if(true) {
           console.log(a)
       } else {
           console.log(2)}}/ / is not an error
  function fn() {
       if(true) {
           console.log(a)
       } else {
           var a
           console.log(2)}}Copy the code

Es6: The code you don’t execute will affect the code I execute. My head is getting too big. Var: “I don’t need to use global variables.” var: “I don’t need to use global variables.” var: “I need to use global variables. Es6: You learn? Look what you learned

    function x() {
        var a = 1
        window.hhh = function() {
            console.log(a)
        }
    }
    
    // not wrapped in a function
    var a = 1
    window.hhh = function() {
        console.log(a)
    }
    
Copy the code

Es6: You expose both HHH and x as global variables, which means you will expose a global variable regardless of whether you use a function or not. Var: I’ll do it like this

    (function x() {
        var a = 1
        window.hhh = function() {
            console.log(a)
        }
    }())
Copy the code

let

Let’s say you don’t like it. I’m just going to do the following

{
    let a = 1
    window.hhh = fubction() {
        console.log(a)
    }
}

 console.log(a) // Error reported without definition
Copy the code

Es6: Yes let as long as you see the {curly braces don’t go up, you look at let, you’re looking at you’re running around unrestrained. I was naive before ES5, but when I met LET later, I realized that all the troubles were caused by myself. If I didn’t meet the right person, I would be very painful. Only through trial and error could I know what I want. Bao: Why did you choose let at the beginning? Es6: AT that time, I was too young to be sensible and the speed was too fast. It took me a week to get well. Later, there were a series of problems. Bao: In order to conveniently use local variables, you chose let, you cheat and play with women’s feelings of man! Es6: Let is so good that its scope is just curly braces.

    {
        let a = 1
    }
    console.log(a) / / an error
Copy the code
    {
        var a = 1
    }
    console.log(a) // a = 1 
Copy the code

Es6: It can also be nested, not as complex as you (with functions)

    {
        let a = 1
        console.log(a) // a = 1 
        {
            console.log(a) / / an error
            let a = 2
            console.log(a) // a = 2
            {
                let a = 3
                console.log(a) // a = 3 }}}Copy the code

Es6: If she uses bolck without declaring the variable, then an error will be reported (Temp Dead Zone)

const

Const: I’m the same as my sister Let

    if(true) {
        let a =1
        console.log(1)}else {
        console.log(2)}console.log(a) / / an error
Copy the code

Const: a to curly braces,let sister declared {}, then her scope is inside curly braces. Es6: That’s right, yes but the fascinating thing about const is that she only happens once, and then she never changes

{
    let a =1
    console.log(a) / / 1
    a = 2   // let a = 2 is not allowed to be declared twice
    console.log(a) / / 2
}
    
{
    const PI = 3.1415926
    PI = 2 / / an error
} 
Copy the code

How to judge this? Bag big head big head…

conclusion

let

  1. Let’s scope is between the nearest {}
  2. If you use a before let a then an error is reported
  3. If you repeatedly declare let a, an error will be reported

Const 1, 2, 3 as above 4. Assign only once and must be done immediately upon declaration