2

I have read my blog about the var and let. What I see is this.

var is a function or global scope variable depend on where it is defined.

where

let is block scope variable

So in lots of articles, I see they recommend to use let instead of var. I understand that because it eliminated the conflict of the scope of a variable.

So I want to ask where to use let and where to use var? If possible please provide any relevant link for that. If I go with recommendation than I have to use let everywhere.

What I understand is this.

let should be used in for loop as it creates its own lexical scope.

for(let i =0;i<5;i++){
  setTimeout(function(){
   console.log(i);  
  },100);
}

So in this case because of let, we will able to print 0,1,2,3,4, but if we have used var it would have print 5 time 5.

Also, I want to know your suggestion on what should be used in function scope and global scope?

let say, I have file index.js

var first = 1; // what should be used here and why

function function1(){
  var first = 1;  // what should be use here and why `let or var`
  var first1 = 2; // what should be use here and why `let or var`
  for(let i=0;i<2;i++){
    console.log(i);
  }

Also, I fill let is more than a variable, I create its own lexical scope, there would be more manipulation under the hood like creating IIFE sort of thing.

What I understand is that we should use function and global scope as var and let as only block scope? Please provide any recommended link which describes what is better where and why?

Thanks.

9

3 Answers 3

2

It's mostly right to only use let these days.

In almost all situations, let is a better or at least equivalent to var considering that leaky declarations make you write error prone code. Avoid using var.

Look at this code:

for(var i = 0; i < 6; i++) {
  document.getElementById('my-element' + i)
    .addEventListener('click', function() { alert(i) })
}

contrary to this code:

for(let i = 0; i < 6; i++) {
  document.getElementById('my-element' + i)
    .addEventListener('click', function() { alert(i) })
}

The first one makes a closure which catches i, defined by var, while the other one makes i different values. The first example makes every callback alert 6, since they are all pointing to the same object. However, the let in the second example makes a new block scope every time it iterates. This solves a very common pitfall in javascript.

In most situations, if you need to use var's scope to achieve something not available using let, it's almost always a sign that's something is wrong.

Also, don't rely on var's variable hoisting. If you are using a variable you didn't declare before, it becomes error prone.

Mostly, try to follow on the style guide you follow. If you don't have a special style guide you follow, try the AirBnB's: https://github.com/airbnb/javascript The "never use var and use let" thing is mentioned here: https://github.com/airbnb/javascript#variables

3
  • Where "better" needs to be qualified. ;-)
    – RobG
    Commented Oct 10, 2018 at 7:19
  • @RobG Well, i tried :-) Can you check if it's better?(I'm a newbie...)
    – user9874253
    Commented Oct 10, 2018 at 7:50
  • 1
    Whenever you write "better" you should be able to immediately write "because…". It's a basic requirement for any technical or formal writing. Your "leaky declaration" is highly contrived and even using let in this case would be poor form due to the obvious confusion the re-use of i is likely to cause.
    – RobG
    Commented Oct 10, 2018 at 8:55
0

let and var

You should use var, when you wan to define a variable globally, or locally to an entire function regardless of block scope. Variables assigned with the var keyword have functional scope. The variable isn’t accessible outside of the function.

And, you should use let to declare variables that are limited in scope to the block, statement, or expression on which it is used. When used inside a block, let limits the variable's scope to that block.

Variables assigned with let are very similar to those defined with var. The major difference is scope. This has been referenced above. Variables assigned with let have block scope. This means the variable can be seen within that block and any sub-blocks. Variables assigned with var have functional scope and ignore block scoping rules. Variables assigned with let can not be redeclared, they can only be reassigned.

According to this article, Only use the let keyword when you know that a variable will have a dynamic value. You probably don’t need to use var anymore

0

Clearly, you know the difference between let and var, and your question is WHERE TO USE WHICH?

First of all, if you are seeking for a better performance, use let wherever you are able to. However, you have to be aware that some old browsers may not be able to handle the let.

Second, I think the best and shortest answer to your question is " use let as long as you can, and if you couldn't then use var".

If you study the differences between let and var deeply, you can figure out where you should use var instead of let, I recommend you to read this article.

2
  • 1
    How is let test; console.log(test) a problem? It works - you don't get any errors. You are allowed to declare a variable and not assign a value to it, it's undefined by default.
    – VLAZ
    Commented Oct 10, 2018 at 7:12
  • 1
    When you need to re-declare your variable, you have to use var because you cannot re-declare the let. and why would you need to re-declare? The variable would be already declared, why would you re-declare it? The var keyword doesn't throw an error when you do that but that doesn't mean it's a correct usage.
    – VLAZ
    Commented Oct 10, 2018 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.