0

What is the difference between using a let in the body of the for loop and a var specifically in the body of a JavaScript for loop? Do they work differently and why?

Example:

Using let

for (let i = 0; i < 10; i++) {

}

Using var

for (var i = 0; i < 10; i++) {

}

Do these work the same way or differently behind the scenes, does one actually functions better?

Does this difference apply to a while loop and a do-while loop too?

Vahe
  • 1,699
  • 3
  • 25
  • 76
  • Try to print the value of I , outside the loops , you'll see the difference – Faizal Hussain Sep 22 '21 at 12:14
  • Perhaps a non-textual answer might be resourceful in this question, to help me see – Vahe Sep 22 '21 at 12:14
  • or https://stackoverflow.com/q/762011/125981 – Mark Schultheiss Sep 22 '21 at 12:14
  • or https://stackoverflow.com/q/111102/125981 – Mark Schultheiss Sep 22 '21 at 12:15
  • Why would printing the value of i outside the for loop scope provide an insight of the difference of value, do you mean for me to print the value before the loop body, and after the loop terminates, or something else? – Vahe Sep 22 '21 at 12:16
  • 1
    This highlights the the effects of `let` when used in the context of for-loops, might be worth having a look at: [Explanation of \`let\` and block scoping with for loops](https://stackoverflow.com/a/30900289) – Nick Parsons Sep 22 '21 at 12:18
  • I need to be able to see this in a contextual picture if possible, I would be happy if such a depiction was provided, or if multiple types, I will provide a up vote. If a bounty needs to be initiated I can, this problem has bothered me for a while. I would be happy to provide an upvote to any sort of answer that adds to an existing answer too. – Vahe Sep 22 '21 at 12:20

1 Answers1

1

let is block scoped and var is not. With let, you cannot use i outside the forloop body. With var (function scoped or globally scoped) you can.

for (var j = 0; j < 10; j++) {

}

console.log(j);


for (let i = 0; i < 10; i++) {

}

console.log(i);

You can run the for loop with var in your chrome console, and will see that i has been attached to the window object.

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
  • Which block is the i in loop 2 scoped to, the region between the curly braces? Or specifically the region between the parentheses... and is like a special for command doing something under the scenes with the three parts of the loop specification. Clearly for seems to resemble a function call but it looks more like a 3 statement section of instructions to proceed... – Vahe Sep 22 '21 at 12:24
  • What is scope in this case, is it only the place where you are confined to, say between curly braces, or even in a global sense, outside curlies? – Vahe Sep 22 '21 at 12:27
  • 1
    Inside the curly braces. That is a block. Although, we can use it inside the parenthesis also. Don't know how for loop construct works under the hood – Tushar Shahi Sep 22 '21 at 12:27
  • So when you move a variable to a window (global scope) based on you description it seems that to be so because it is declared as a var, but is there something like a variable hoisting happening? If this is the case, why would we want to use a var at all? – Vahe Sep 22 '21 at 12:31