How to use the for loop In Javascript
We can iterate in the list using a simple for loop and then append the list item using appendChild.
Example:
<!DOCTYPE html>
<html lang="en">
<body>
<center>
<h1>w3wiki</h1>
</center>
<ul id="myList"></ul>
<script>
let data = ["Ram", "Shyam", "Sita", "Gita"];
let list = document.getElementById("myList");
for (i = 0; i < data.length; ++i) {
let li = document.createElement('li');
li.innerText = data[i];
list.appendChild(li);
}
</script>
</body>
</html>
Output:
How to create HTML list from JavaScript array ?
Creating an HTML list from a JavaScript array involves dynamically generating <ul> or <ol> elements and their corresponding <li> items. By iterating through the array and appending each element as a list item, you can display array data as a structured HTML list.
Below are the ways to create the list from the Javascript array:
Table of Content
- Using the for loop
- Using forEach() loop
- Using join() Method