1

Question title seems straight forward but that's not what I am asking. Please try following examples

for(let i=0; i<5; i++){
 //console.log(i);
 let i=5 
 console.log(i);
}

This example works and my question is how is it possible to declare another variable with same identifier inside for loop.

I am really confused, what is going on??

RakeshRaj
  • 69
  • 9
  • 1
    It seems that it's the *opposite* of "never ending" - it terminates after a single iteration. – Oliver Charlesworth May 08 '18 at 19:13
  • Wouldn't the opposite be "never starting"? – Dave Newton May 08 '18 at 19:16
  • 2
    Possible duplicate of [What's the difference between using "let" and "var" to declare a variable in JavaScript?](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable-in-jav) – Christian Benseler May 08 '18 at 19:19
  • @DaveNewton, now it is never ending (changed the value to 2), and I now know why it is never ending, basically it's never ending loop now. – RakeshRaj May 08 '18 at 19:36
  • @ChristianBenseler no it not duplicate, my question is based on the code snippets I posted. – RakeshRaj May 08 '18 at 19:43
  • 2
    Questions can be duplicates of each other even if the code is different. –  May 08 '18 at 20:27

4 Answers4

5

For the semantics of let in for loops, see Explanation of `let` and block scoping with for loops.

How is it possible to declare another variable with same identifier inside for loop?

It's because your for loop has a block statement as its body, and in there you can declare your own block-scoped variables, shadowing the ones from parent scopes.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Yes, if what you said is true, it totally makes sense. Is there any reference to what you said (MDN??) where I can explore more? – RakeshRaj May 08 '18 at 19:46
  • @Raaz I don't know any good references to link, sorry. My knowledge comes directly from the spec, which I cannot recommend to read. What else would you like to know? – Bergi May 08 '18 at 19:53
0

In the following example you set the value of i to 5 so the loop ends coz that's the condition for the loop i<5. So condition is false for the loop to run again

for(let i=0; i<5; i++){
 //console.log(i);
 i=5 
 console.log(i);
}

And in your firs example, loop continues coz let i =5 is considered a separate variable in the for loop scope

Muhammad Usman
  • 10,039
  • 22
  • 39
  • Actually, I modified the question little bit, I changed it to 2 (something smaller than 5) and yes, it makes sense now the loop runs forever. I got that part. But what about my first question? How is it possible to declare a variable with same identifier within the scope? – RakeshRaj May 08 '18 at 19:23
  • and that's causing this page to crash. :/ – Muhammad Usman May 08 '18 at 19:23
  • "considered a separate variable??" this does not makes sense to me. How two variables with same identifier considered different in same scope?? That brings back the question what is the actual scope of let?? – RakeshRaj May 08 '18 at 19:31
0

I think below code will remove all your doubt It is all about scope, You can not re-define same variable in same scope but you can define in nested scope

Same thing happening in your code for has own scope and inside for means

for { //inside this } is  nested scope of `for`.

{
    let a = "a";
    console.log(a);
    {
        let a = "b";
        console.log(a);
    }
    console.log(a);
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29
  • Thanks Nishant, yes, if it is nested block it's a simple thing to understand, can you point me to the reference or documentation where this behavior of for's nested scope is explained? – RakeshRaj May 08 '18 at 19:48
  • @Raaz http://www.benmvp.com/learning-es6-block-level-scoping-let-const/ this link contains detail explanation about `var`, `let ` and `const`. – Nishant Dixit May 09 '18 at 05:11
0

It seems like a lot of people are talking around the real point of confusion here...

Ok, so when you say

// scope A
for(let i = 0; i<5; i++) {
    //scope B
}

we know i is not declared in Scope A. (That's one of the big problems let solves.) So it seems it must be declared in Scope B. But what about this case?

// scope A
for(let i = 0; i<5; i++) console.log(i);

The for loop still has to have its own scope, in order to keep i from being in Scope A. And we can demonstrate this.

let i = 37;
for(let i = 0; i < 5; i++) console.log(i);

console.log(i); // prints 37

Now if the for statement already has a scope even though it has no braces, then what happens when we replace console.log(i) with a block statement? Well, that block statement gets its own scope. So really we have

// Scope A
for( //scope B is actually here
     let i = 0; i < 5; i++) {
    // Scope C
}
Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52