What is the difference between var and let in Typescript?

后端 未结 4 1510
旧时难觅i
旧时难觅i 2020-12-28 12:13

I submitted a question on stack overflow asking how I could stop the putTestQuestionResponses() function from executing IF a previous version was already executing.

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-28 12:27

    function varTest() {
      var x = 1;
      if (true) {
        var x = 2;  // same variable!
        console.log(x);  // 2
      }
      console.log(x);  // 2
    }
    
    function letTest() {
      let x = 1;
      if (true) {
        let x = 2;  // different variable
        console.log(x);  // 2
      }
      console.log(x);  // 1
    }
    

    I found this here

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

提交回复
热议问题