javascript - ES6 declaring variables before or in loop - Stack Overflow

Where and how should I declare new variables used in loops?A:const map = new Map(Object.entries(columns

Where and how should I declare new variables used in loops?

A:

const map = new Map(Object.entries(columns));
let cols;
for (let [key, value] of map)
{
    cols = value.split('|');
    //...
}

B:

const map = new Map(Object.entries(columns));
for (let [key, value] of map)
{
    let cols = value.split('|');
    //...
}

C:

const map = new Map(Object.entries(columns));
var cols;
for (let [key, value] of map)
{
    cols = value.split('|');
    //...
}

Probably A or B since everyone says let is the new var, but is there any difference between A and B?

Edited:
The variable cols will be used only inside for. I was wondering if there are some issues if variable is initialized inside loop (for example 100 times). So I wondered if it should be initialized outside loop. (A or B example)

The purpose is not to get access outside loop, but prevent (for example) 100 initialization variable cols inside loop (because let is used inside loop - case B).

Where and how should I declare new variables used in loops?

A:

const map = new Map(Object.entries(columns));
let cols;
for (let [key, value] of map)
{
    cols = value.split('|');
    //...
}

B:

const map = new Map(Object.entries(columns));
for (let [key, value] of map)
{
    let cols = value.split('|');
    //...
}

C:

const map = new Map(Object.entries(columns));
var cols;
for (let [key, value] of map)
{
    cols = value.split('|');
    //...
}

Probably A or B since everyone says let is the new var, but is there any difference between A and B?

Edited:
The variable cols will be used only inside for. I was wondering if there are some issues if variable is initialized inside loop (for example 100 times). So I wondered if it should be initialized outside loop. (A or B example)

The purpose is not to get access outside loop, but prevent (for example) 100 initialization variable cols inside loop (because let is used inside loop - case B).

Share Improve this question edited Jul 28, 2016 at 15:09 Makla asked Jul 28, 2016 at 10:45 MaklaMakla 10.5k18 gold badges81 silver badges155 bronze badges 3
  • You should always use the narrowest possible scope this is, example B. – user6445533 Commented Jul 28, 2016 at 11:21
  • "everyone says let is the new var" - beat everyone with a stick who says that without qualification. – Bergi Commented Jul 28, 2016 at 16:49
  • "prevent 100 initialization variable cols inside loop" - that is the piler's job. You don't need and should not do it. – Bergi Commented Jul 28, 2016 at 16:51
Add a ment  | 

2 Answers 2

Reset to default 4

In code snippet A, cols is accessible outside of the for too. As let variables are block-scoped, when used let to define variable inside for, the scope of the variable is for that block only. So, in B, the variable cols will not be accessible outside of the for.

C, is similar to A if cols is defined only once. If col is defined twice in the same scope using let will result in error.

Which one to use depends on the use-case.

  1. If cols is needed inside for only, then use let cols = ...
  2. If cols is needed outside of for too, use let cols; before for and then it can be used after for too in the same enclosing scope. Note that, in this case, cols will be the last value assigned in the loop.

The difference between A and B is the scope of the var. In first case you can access cols also outside the loop, in the second you can't.

let a;

for (let i =0; i < 2; i++) {
  let b = i;
  a = i;
}

console.log('a outside the loop:', a);
console.log('b outside the loop:', b);
console.log('i outside the loop:', i);  //If `var` was used instead of `let` then `i` would have had global scope

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745279376a4620205.html

相关推荐

  • javascript - ES6 declaring variables before or in loop - Stack Overflow

    Where and how should I declare new variables used in loops?A:const map = new Map(Object.entries(columns

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信