Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between var and let in Typescript?

Tags:

typescript

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);
}]
like image 544
Alan2 Avatar asked Feb 23 '16 08:02

Alan2


People also ask

What is the difference between VAR and let?

let is block-scoped. var is function scoped. let does not allow to redeclare variables. var allows to redeclare variables.

When use let or VAR or const TypeScript?

Use var and let to define any variable, with or without type or initial value. We use the const keyword initialize a constant whose value does not change. Hence we must initialize it with a value. You can not use it to declare a variable where there is no initial value.

Which is better let or VAR?

This is because both instances are treated as different variables since they have different scopes. This fact makes let a better choice than var . When using let , you don't have to bother if you have used a name for a variable before as a variable exists only within its scope.

Why let is used in TypeScript?

The let statement is used to declare a local variable in TypeScript. It is similar to the var keyword, but it has some restriction in scoping in comparison of the var keyword. The let keyword can enhance our code readability and decreases the chance of programming error.


5 Answers

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

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

like image 58
Martin Vseticka Avatar answered Oct 11 '22 08:10

Martin Vseticka


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
like image 26
wyl Avatar answered Oct 11 '22 07:10

wyl


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
like image 36
chenchu kotari Avatar answered Oct 11 '22 08:10

chenchu kotari


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

like image 43
Dharmendra Prajapati Avatar answered Oct 11 '22 06:10

Dharmendra Prajapati


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.

like image 39
R15 Avatar answered Oct 11 '22 08:10

R15