question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong behaviour in "for...of" statement

See original GitHub issue

Bug 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:closed
  • Created 5 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Oct 11, 2018
0reactions
liuxingbaoyucommented, Oct 30, 2022

Seems to have been fixed, feel free to open an issue if the problem persists.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found