admin管理员组

文章数量:1225076

I am using const with the new for of looping structure of JavaScript. It works fine in Chrome but in MS Edge the following code throws error:

for(const a of [1, 2, 3])
    console.log(a);

Error: Const must be initialized

Again, works fine in chrome, edge throws error. I guess it expects const variable to have an initialization value but that's the entire job of the for isn't it?

MDN says edge supports the loop so browser support isn't an issue.

I am using const with the new for of looping structure of JavaScript. It works fine in Chrome but in MS Edge the following code throws error:

for(const a of [1, 2, 3])
    console.log(a);

Error: Const must be initialized

Again, works fine in chrome, edge throws error. I guess it expects const variable to have an initialization value but that's the entire job of the for isn't it?

MDN says edge supports the loop so browser support isn't an issue.

Share Improve this question asked Jun 13, 2016 at 21:47 AchsharAchshar 5,2438 gold badges42 silver badges72 bronze badges 3
  • It is perfectly valid ES2015 code, so I presume it's the browser that should be blamed. – zerkms Commented Jun 13, 2016 at 21:50
  • Yeah I guess it's an undocumented behavior. – Achshar Commented Jun 13, 2016 at 21:52
  • const is relatively new for JavaScript. I agree with zerkms. – Joshua Commented Jun 13, 2016 at 22:18
Add a comment  | 

1 Answer 1

Reset to default 18

According to https://kangax.github.io/compat-table/es6, "const in for-of loop iteration scope" is not supported in IE and not in Edge until version 14. It's the same with let btw. Basic for of loops, and basic const/let usage do work though. MDN is not the most accurate source for browser support.

本文标签: javascriptConst must be initialized error in Microsoft Edge in forof loopStack Overflow