TypeError: redeclaration of let error in Firebug console if running ES6 code

纵然是瞬间 提交于 2019-12-22 04:08:25

问题


I am learning ES6, so bear me please.

Following is the code which is running fine, if I click the Run button one time, but on second hit it starts showing TypeError: redeclaration of let myArr error.

Let me know about this weird (may be not) behavior.

let myArr = [34,45,67,2,67,1,5,90];
let evenArr = [];
let oddArr = [];
myArr.forEach(x => {
    if (x % 2 === 0) {
        evenArr.push(x);
    } else {
        oddArr.push(x);
    }
});

console.log(evenArr);
console.log(oddArr);

Error -


回答1:


ES6 does not allow you to do this (redeclaring a block-scoped variable in the same scope):

let foo;
let foo;

And since the console keeps state until you reload the page (you are in the context of the page after all), the first time you run it you define myArr so you cannot redefine it on the second run.



来源:https://stackoverflow.com/questions/36398258/typeerror-redeclaration-of-let-error-in-firebug-console-if-running-es6-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!