How to Dynamically Generate and Display Lists from JavaScript Arrays

I am going to show how to generate a list from an array dynamically with the help of JavaScript. We will create a simple unordered list in HTML, then populate it from a JavaScript array using forEach, document.createElement, and appendChild.

Setup HTML for JavaScript Array to List

  How to Dynamically Generate and Display Lists from JavaScript Arrays  1   Create a starting HTML file and add an unordered list with an ID. We will populate this list from the array.
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>JavaScript Array to List</title>
  </head>
  <body>
    <ul id="myList"></ul>

    <script>
      // JavaScript will go here
    </script>
  </body>
</html>
  How to Dynamically Generate and Display Lists from JavaScript Arrays  2  

Create the array and reference the list

  How to Dynamically Generate and Display Lists from JavaScript Arrays  3   Inside the script tag, start by creating an array of items. Then get the unordered list element by its ID.
<script>
  const items = ["apple", "banana", "grapes", "mango"];

  const list = document.getElementById("myList");
</script>
  How to Dynamically Generate and Display Lists from JavaScript Arrays  4  

Build the JavaScript Array to List with forEach

  How to Dynamically Generate and Display Lists from JavaScript Arrays  5   Put a forEach loop on the array to iterate over all the items. For each item, create an li, set its textContent, and append it to the ul.
<script>
  const items = ["apple", "banana", "grapes", "mango"];
  const list = document.getElementById("myList");

  items.forEach(item => {
    const li = document.createElement("li");
    li.textContent = item;
    list.appendChild(li);
  });
</script>
  How to Dynamically Generate and Display Lists from JavaScript Arrays  6  

Full example

Here is the complete working example with HTML and JavaScript together.
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>JavaScript Array to List</title>
  </head>
  <body>
    <ul id="myList"></ul>

    <script>
      const items = ["apple", "banana", "grapes", "mango"];

      const list = document.getElementById("myList");

      items.forEach(item => {
        const li = document.createElement("li");
        li.textContent = item;
        list.appendChild(li);
      });
    </script>
  </body>
</html>
  How to Dynamically Generate and Display Lists from JavaScript Arrays  7  

How it works

Every time the loop runs, it creates a list item and puts the current array element inside that list item. Then it appends this li to the unordered list. The list is the ul with the ID myList. If the JavaScript is not present, the unordered list remains completely blank. After adding the JavaScript, the list shows apple, banana, grapes, and mango.

Final thoughts

This is how we can create the list using the array dynamically. The key parts are getElementById to grab the list, forEach to iterate over the array, createElement to build each li, textContent to set its text, and appendChild to add it to the ul.

Leave a Comment