69

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

The reply was to add in a processing flag which is here on line 2 of this code.

Can you tell me why use a "let" instead of a "var" here?

var promisePutTestQuestion;
let processing = false;

onEnter: ['$interval', 'questionService',
         ($interval, qus: IQuestionService) => {
  promisePutTestQuestion = $interval(() => {
     if (processing)
         return;
     processing = true;
     qus.putTestQuestionResponses()
     .then(() => processing = false)
  }, 5 * 1000);
}],
onExit: ['$interval', ($interval) => {
        $interval.cancel(promisePutTestQuestion);
}]
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 2
    I recommend to read https://basarat.gitbooks.io/typescript/content/docs/let.html – MartyIX Feb 23 '16 at 08:55
  • this seems also to be duplicate of http://stackoverflow.com/questions/32051173/var-and-let-in-typescipt-1-5 – ciekawy Feb 23 '16 at 08:57
  • http://stackoverflow.com/questions/762011/let-keyword-vs-var-keyword – Ryan Cavanaugh Feb 23 '16 at 23:25
  • Possible duplicate of [What's the difference between using "let" and "var" to declare a variable?](http://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable) – Ramesh Rajendran Apr 13 '17 at 09:30

5 Answers5

90

var declaration is function scoped and let declaration is block scoped.

See https://basarat.gitbooks.io/typescript/content/docs/let.html for more details.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
  • 4
    Coming from a C# background this seems weird. Should there not be a single keyword for variable declarations? Why would we have non-block scoped variables? Seems to make little sense.. but I am just starting to learn TypeScript. – Web Dev Mar 13 '18 at 06:32
  • 5
    `var` comes from JavaScript where it is a function scoped declaration. TypeScript does not have much of a choice to change the behavior. – MartyIX Mar 13 '18 at 12:51
  • 3
    I see. I guess that is because TypeScript supports plain JavaScript as well. Make sense. So what you are saying is that it is best to avoid the var keyword and instead use the let keyword, at least when trying to design code in a similar style as C#, Java, etc, where all variables are scoped to their current scope? – Web Dev Mar 13 '18 at 19:49
  • 7
    Yes, `let` is preferred way. (TypeScript looks similar to C# but it's always better to reason about TypeScript in context of JavaScript as there are many fundamental differences between JavaScript and .NET languages (prototyping, static types, etc.)) – MartyIX Mar 14 '18 at 11:27
  • `const` should be neither `block` nor `function` because the variable value cannot change. – Timo Sep 16 '22 at 10:42
40

example:

// demo: var
for(var i =0; i<5; i++){
   console.log(i) 
}//finally i =5
console.log(i) // i=5

// demo: let 
for(let i = 0; i<5; i++){
   console.log(i)
}
console.log(i)// i is undefined
wyl
  • 408
  • 3
  • 6
39

var variables in JavaScript are function scoped. This is different from many other languages (C#, Java, etc.) where the variables are block scoped. If you bring a block scoped mindset to JavaScript, you would expect the following to print 123, instead it will print 456:

var foo = 123;
if (true) {
    var foo = 456;
}

console.log(foo); // 456

This is because { does not create a new variable scope. The variable foo is the same inside the if block as it is outside the if block. This is a common source of errors in JavaScript programming. This is why TypeScript (and ES6) introduces the let keyword to allow you to define variables with true block scope. That is, if you use let instead of var, you get a true unique element disconnected from what you might have defined outside the scope. The same example is demonstrated with let:

let foo = 123;
if (true) {
    let foo = 456;

}

console.log(foo); // 123
John Montgomery
  • 6,739
  • 9
  • 52
  • 68
chenchu kotari
  • 571
  • 5
  • 2
4
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

4

It's all about accessibility. If you use let then it will be accessible under that scope only not outside of the function, if, else scope. but var we can access outside of the for, if, else.

See below code

public selectedLocation(country)
  {  
     if(instance==this.list[0])
    {
      var obj=this.productArray
    }
    
    for(let i = 0; i < this.obj.length; i++)
    {
       obj=this.productPending
    }
  }

Above code is working with var obj but this will not work with let obj for the for loop.

R15
  • 13,982
  • 14
  • 97
  • 173