How to print array one by one in using button js | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to print array one by one in using button js

JavaScript

23rd Apr 2021, 3:51 PM
Ashish Hojai
Ashish Hojai - avatar
4 Answers
+ 2
You can use a simple for loop. Use the document.writeln to print it out. See the below working snippet. array = ["example1", "example2", "example3"]; for (i = 0; i < array.length; i++) document.writeln((i+1) + ": " + array[i]);
23rd Apr 2021, 4:05 PM
Matias
Matias - avatar
+ 1
You may use. For loop For..in loop For..of loop
23rd Apr 2021, 3:54 PM
TOLUENE
TOLUENE - avatar
+ 1
Thanks
24th Apr 2021, 9:35 AM
Ashish Hojai
Ashish Hojai - avatar
0
There are several ways. Seeing as for loops and for of loops have been mentioned, I'll add forEach to the list. For the outputting itself, you can use console.log, document.write or another couple of methods, depending on your needs. Here's forEach: ["a", "b"].forEach(item => console.log(item))
23rd Apr 2021, 5:01 PM
CamelBeatsSnake
CamelBeatsSnake - avatar