The Wayback Machine - https://web.archive.org/web/20240116185922/https://www.geeksforgeeks.org/how-to-creating-html-list-from-javascript-array/
Open In App
Related Articles

How to create HTML list from JavaScript array ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will be creating an HTML list from a JavaScript array. This is needed sometimes when we fetch JSON from any source and display the data in the front end and in many other cases. 

Below are the ways to create the list from the Javascript array:

Method 1: Using the for loop

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>GeeksforGeeks</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:

Method 2: 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. 

Syntax:

array.forEach(callback(element, index, arr), thisValue)

Example:

HTML




<!DOCTYPE html>
<html lang="en">
 
<body>
    <center>
        <h1>GeeksforGeeks</h1>
    </center>
    <ul id="myList"></ul>
 
    <script>
        let data = ["Ram", "Shyam",
            "Sita", "Gita"];
        let list =
            document.getElementById("myList");
 
        data.forEach((item) => {
            let li =
                document.createElement("li");
            li.innerText = item;
            list.appendChild(li);
        });
    </script>
</body>
 
</html>


Output:

Method 3: 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(, ).

Syntax:

array.join(separator)

Example:

Javascript




<!DOCTYPE html>
<html lang="en">
 
<body>
    <center>
        <h1>GeeksforGeeks</h1>
    </center>
    <ul id="myList"></ul>
 
    <script>
        // JavaScript array
        let data = ["Ram", "Shyam",
            "Sita", "Gita"];
 
        // Get the list container element
        let list =
            document.getElementById('myList');
 
        // Create the unordered list element
        //and set its inner HTML using map() and join()
        let ul = `<ul>${data.map(data =>
            `<li>${data}</li>`).join('')}
                  </ul>`;
 
        // Set the inner HTML of the list container
        list.innerHTML = ul;
    </script>
</body>
 
</html>


Output:


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 07 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials