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:

HTML
<!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

Similar Reads

Using the for loop

We can iterate in the list using a simple for loop and then append the list item using appendChild....

Using forEach() loop

The arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array....

Using join() Method

The JavaScript Array join() Method is used to join the elements of an array into a string. The elements of the string will be separated by a specified separator and its default value is a comma(, )....