2

As far as I know, 'let' is used for declaring block variable. But it can't declare twice with same name. For example:

'use strict';
let a = 1;
let a = 2; // syntax error

So how to separate each scope in 'for' iteration with the same variable name?

'use strict';
for(let i = 0; i < 3; i++){
    setTimeout(function(){
        console.log(i);
    });
}
// output: 
// 0
// 1
// 2

Dose interpreter change the variable name silently? Any info will be appreciated.

I don't think is a duplicated question. Because I really want to ask is the conflict between two theory.

JamesYin
  • 732
  • 9
  • 19
  • I don't think is duplicated. I want to know the conflict between two principles. – JamesYin Aug 10 '17 at 09:46
  • You can try running your code in [Babel Repl](https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-0&targets=&browsers=&builtIns=false&debug=false&code_lz=GYewTgFANgpgLgAgJYILwIAwG5kIDwIDMOSA1KQJQDeAsAFAKMIDO8AKkgLYwgCucEYLwB2AYzhIQwiNXpN5CUVOYhYAOiggA5hCQUscpgF999I0A) to get an idea of how it works. – Siddhant Patra Aug 10 '17 at 09:58

7 Answers7

0

let scopes your variable to the block it's in. After you've assigned a scope to a, you can't overwrite it. So the second time you use let, it can't handle that.

Correct usage would be:

'use strict';
let a = 1;
a = 2;
Soronbe
  • 906
  • 5
  • 12
  • 1
    The question is not about the first code block, but how it works in a for loop. – Ivar Aug 10 '17 at 09:45
  • 1
    The first code is OP stating that he understands how `let` works. He is asking for `let` in the `for` loop. – yuriy636 Aug 10 '17 at 09:45
0

In the for loop , i is declaring at the beginning only and in each iteration it change the value of it .

let is used to variable that you will write there value again .

In the first example you define variable with name 'a' so you cant define another with same name , but you can change it value , which happened in the for loop.

ZAhmed
  • 1,232
  • 8
  • 15
  • 2
    Not true. As explained in [this answer](https://stackoverflow.com/questions/16473350/let-keyword-in-the-for-loop), every iteration `i` will get a new binding. – Ivar Aug 10 '17 at 10:09
0
'use strict';
let a = 1;
let a = 2; // syntax error

'use strict';
let a = 1;
var a = 2; this will overwrite...

here you specified a value already.. your intention is , it shouldn't change in runtime. so you cant able to change it once declared in block with same type let ..

that why you getting this error..

intention of let is value should not change..

in loop , it will be separate on iterate.. so it print value on runtime. so it wont overwrite it...

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
0

let allows to create the variable once, but the value of the variable can be changed, example -

     let t =5// 
    let t = 6 // throws an errow but 
    t =7 
 console.log(t) - gives 7
Vinod kumar G
  • 639
  • 6
  • 17
0

In your first example the variable a is defined twice in the same scope. This gives you an error because the variable already exists. let doesn't allow this, redeclaring with var does. This is one advantage of using let.

var a = 3;
var a = 2;

console.log(a); // 2

let b = 3;
let b = 2; //syntax error

let c = 3;
var c = 2; //also a syntax error

In a for loop, the i variable has a different scope. Meaning these two i variables here aren't the same.

let i = "my string";
console.log(i); //my string

for(let i = 1; i <= 3; ++i){
  console.log(i); //1, 2, 3
  
}

console.log(i); //my string

The javascript runtime doesn't care if your variable is named the same. It will differentiate the scope and thus the reference to the variable. It's replacing your reference to i whichs value is 4 to the new variable in the for loop.

I have an answer on here on Stackoverflow which describes how the Android compiler doesn't care about your variable names. It's the same here, the runtime uses different "names" (different memory addresses).

Spitzbueb
  • 5,233
  • 1
  • 20
  • 38
0

In the loop the variable is not declared again but only its value is incremented. It is similar to the following:

'use strict';
 let i = 0;
 i = i + 1;

consider the code being iterated again and again until the condition fails.

Parthipan Natkunam
  • 756
  • 1
  • 11
  • 16
0

for(let i = 0; i < 3; i++){ // code }

step:

  1. let i = 0

  2. checkout i<3

  3. exec code

  4. i++, equals i = i + 1 // not occur error, while let i = i + 1 will occur exception

  5. loop step-2 until i<3 == false

X.Nane
  • 46
  • 2
  • 8