I want to Write a program that prints numbers from 1 to 1000 Using For Loop. All numbers should be printed in the same line separated by comma and space (like this: 1, 2, 3).but i cant figure it out how to do it in for loop I am a beginner please help.
Asked
Active
Viewed 315 times
1 Answers
2
You can try the Spread syntax
to create the array and then use keys to join with Array.prototype.join()
:
console.log([...Array(1000).keys()].join(', '));
OR: You can use Array.from()
method which creates a new, shallow-copied Array instance from an array-like or iterable object:
console.log(Array.from(Array(1000).keys()).join(', '));

Mamun
- 66,969
- 9
- 47
- 59
-
1This should be the accepted answer and not the one linked as duplicate – A. Llorente Jun 27 '18 at 10:54