Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use let and var? [closed]

EDIT: Please read the question! I already know the difference. This is not a duplicate.

Obviously, right now I should always be using the var key word as let isn't supported in everything.

When the let keyword has better support (say, I'm writing a Node application in a couple years time which uses Harmony), when should I use the let keyword vs the var keyword?

I understand the difference —var is for function scoping while let is for block scoping—but I'm looking for something like "always use the let keyword" or "use the var keyword at the top of functions, but the let keyword in blocks like for loops".

like image 753
callumacrae Avatar asked Feb 20 '14 11:02

callumacrae


People also ask

When should I use var instead of let?

let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

Why might you use the let keyword instead of VAR '?

let is block-scoped In the above program, the variable a is declared inside the function and it can be accessed anywhere inside the function ( a becomes function scoped). However the variable b is declared inside the if block statement. b will be block-scoped and can only be accessed inside the if block.

What is correct about VAR and let?

var and let are both used for variable declaration in javascript but the difference between them is that var is function scoped and let is block scoped. It can be said that a variable declared with var is defined throughout the program as compared to let. An example will clarify the difference even better.


2 Answers

I would say that you should, as a principle, use let whenever it is not inconvenient to do so. Such as:

for (let i = 0; i < 100; i++) {     // Do something }  if (condition) {     let msg = a + b + c;     console.log(msg);     alert(msg); } 

The advantages to this approach is:

  1. Less risk of overriding some global variable use for something else
  2. Less risk of memory leaks due to variables staying in memory long after they have become irrelevant
like image 65
neelsg Avatar answered Sep 24 '22 00:09

neelsg


Use let as a general rule, and var on occasion.

Block scoping is the standard and most readable choice, and will make debugging easier. Block scoping makes it easy to see exactly where a variable is in scope. Function scoping makes things a lot less apparent, and much easier to accidentally introduce bugs with scoping mistakes.

In general, the smaller the scope you can use, the better. Thus let over var.

In particular, it helps deal with the endless problem of closing over variables and not realising their value will change before the closure is executed:

for (var i = 1; i <= 5; i++) {   var item = document.createElement("LI");   item.appendChild(document.createTextNode("Item " + i));    let j = i;   item.onclick = function (ev) {     alert("Item " + j + " is clicked.");   };   list.appendChild(item); } 
like image 23
Phil H Avatar answered Sep 23 '22 00:09

Phil H