Monday, June 3, 2024
114
rated 0 times [  115] [ 1]  / answers: 1 / hits: 18912  / 8 Years ago, mon, april 4, 2016, 12:00:00

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 -



redelaration


More From » ecmascript-6

 Answers
19

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.


[#62703] Friday, April 1, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gageherberth

Total Points: 249
Total Questions: 115
Total Answers: 119

Location: Liechtenstein
Member since Sun, Sep 12, 2021
3 Years ago
;