This is an error I've never seen before.
Here's a simple repro: /
const x = myVar => {
console.log(myVar);
for(let myVar of [1,2,3]){
console.log(myVar);
}
};
x(10);
Output in Chrome: 10
, 1
, 2
, 3
Output in Safari: SyntaxError: Cannot declare a let variable twice: 'myVar'.
Ideas? Is this a bug in Safari?
Edit - Worth noting, I wouldn't ever do this on purpose. I noticed it because I use the ASP.NET bundling & minification system, and that system did this (so my site broke in Safari)
Edit 2 - Interestingly, this works in both browsers
let x = 10;
for(let x of [1,2,3])
{
console.log(x);
}
/
This is an error I've never seen before.
Here's a simple repro: https://jsfiddle/jakelauer/qr0ysmst/3/
const x = myVar => {
console.log(myVar);
for(let myVar of [1,2,3]){
console.log(myVar);
}
};
x(10);
Output in Chrome: 10
, 1
, 2
, 3
Output in Safari: SyntaxError: Cannot declare a let variable twice: 'myVar'.
Ideas? Is this a bug in Safari?
Edit - Worth noting, I wouldn't ever do this on purpose. I noticed it because I use the ASP.NET bundling & minification system, and that system did this (so my site broke in Safari)
Edit 2 - Interestingly, this works in both browsers
let x = 10;
for(let x of [1,2,3])
{
console.log(x);
}
https://jsfiddle/jakelauer/aw37pd2s/1/
Share Improve this question edited Feb 25, 2017 at 0:47 Jake asked Feb 25, 2017 at 0:27 JakeJake 4,23410 gold badges37 silver badges56 bronze badges 6-
1
I'm not sure whether this should be an error or not, but I think it's bad coding to shadow a parameter like that. You should call the
myVar
variable in your loop something else. – Freyja Commented Feb 25, 2017 at 0:30 - @Frxstrem I certainly agree - I wouldn't ever do it on purpose. I noticed it because I use the ASP.NET bundling & minification system, and that system did this (so my site broke in Safari). – Jake Commented Feb 25, 2017 at 0:39
- What Safari version? – Andrew Li Commented Feb 25, 2017 at 0:40
- @AndrewLi I'm using 10.0.3 – Jake Commented Feb 25, 2017 at 0:41
-
4
Definitely seems like a Safari bug to me. The
let
in the loop should be running in its own iteration-specific environment. – loganfsmyth Commented Feb 25, 2017 at 0:42
2 Answers
Reset to default 5Looks like it's a Safari bug. I filed a bug report.
There is a mention of 'shadow' variables, but this is the case that tripped things up for me:
I am reusing key
inside the loop and 'shadowing' the parameter for the function.
I was able to get both these errors:
cannot declare a const variable twice
or
cannot declare a let variable twice
by changing the declaration in the for loop.
So the fix for me was easy - to just change const key
to const k
for the loop.