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??

6
  • 1
    It seems that it's the opposite of "never ending" - it terminates after a single iteration. Commented May 8, 2018 at 19:13
  • Wouldn't the opposite be "never starting"? Commented May 8, 2018 at 19:16
  • 2
    Possible duplicate of What's the difference between using "let" and "var" to declare a variable in JavaScript? Commented May 8, 2018 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. Commented May 8, 2018 at 19:36
  • 2
    Questions can be duplicates of each other even if the code is different. Commented May 8, 2018 at 20:27

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@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?
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

3 Comments

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?
and that's causing this page to crash. :/
"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??
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);
}

2 Comments

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?
@Raaz benmvp.com/learning-es6-block-level-scoping-let-const this link contains detail explanation about var, let and const.
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
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.