0

Is there a good way to do a for loop with a for each loop in javascript?

I want to make a for loop in one line.

2 Answers2

2

Or ....

Array(9).fill(0).forEach((_,i) => console.log(i));
adeneo
  • 293,187
  • 26
  • 361
  • 361
  • Its a good code. But could you explain those fill and `_` and all? – jafarbtech Sep 20 '17 at 19:36
  • 1
    `Array.fill` just fills the array with zeroes so it can be iterated, and the underscore is just "filler" for the uneeded argument, I just used `_` instead of your `e` – adeneo Sep 20 '17 at 20:49
0

Try this:

(new Array(int + 1).join("0").split("")).forEach((e, i) => {console.log(i);})

int means iterate times