Wrong behaviour in "for...of" statement
See original GitHub issueBug Report
Current Behavior Throws this exception:
TypeError: undefined: Duplicate declaration "i"
1 |
2 | for (let i of 'foo') {
> 3 | let i = 'a';
| ^
4 | console.log(i);
5 | }
6 |
Input Code
for (let i of 'foo') {
let i = 'a';
console.log(i);
}
Expected behavior/code
Should prints 'a'
three times instead of throws this exception.
This is the behaviour in Node and Safari, for example.
Babel Configuration (CLI command)
const output = require('@babel/core').transform(`
for (let i of 'foo') {
let i = 'a';
console.log(i);
}
`, {
presets: ['@babel/preset-env']
});
Environment
- Babel version(s): lastest
- Node/npm version: Node 9
- OS: macOS High Sierra
- Monorepo: yes
- How you are using Babel:
cli
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
for...of - JavaScript | MDN - MDN Web Docs
An iterable object. The source of the sequence of values on which the loop operates. statement. A statement to be executed on every...
Read more >Inconsistent scope rules of variables in for, for-in and for-of loops
My question is whether this behaviour is expected and defined in the spec somewhere? MDN doesn't say much about this. javascript · for-loop ......
Read more >Using 'ForOfStatement' is not allowed (no-restricted-syntax)
A clear statement of 'This ESLint preset prefers Array.forEach over for of because the polyfill for for of is too heavyweight for the...
Read more >JavaScript Loops Explained: For Loop, While Loop, Do...while ...
Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions typically return true or false. A loop will continue ......
Read more >For vs forEach() vs for/in vs for/of in JavaScript
This is why iterating through an array using for/in is generally bad practice. The other looping constructs correctly ignore the num-numeric ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s a bug, similar to https://github.com/babel/babel/issues/8498
Seems to have been fixed, feel free to open an issue if the problem persists.