To view Google ads on a website, activate JavaScript in your browser. Follow the instructions provided by your browser: Browser upgrades, security software, or patches may change your browser’s JavaScript setting. If you have problems viewing Google ads, confirm that JavaScript is activated in your browser. If the JavaScript setting is set to “Prompt,” websites that show Google ads may display a warning. Was this helpful? How can we improve it?Activate JavaScript in Google Chrome
Activate JavaScript in another browser
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
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 displaying the data into the frontend and in many other cases also.
Problem statement: Display the array [‘Ram’, ‘Shyam’, ‘Sita’, ‘Gita’ ] into an HTML list.
To achieve this, we will follow the steps below.
Step 1: Create the HTML skeleton.
HTML
GeeksforGeeks
Step 2: Create a variable named list and get the element whose id is “myList”.
Javascript
let list = document.getElementById[
"myList"
];
Step 3: Now iterate all the array items using JavaScript forEach and at each iteration, create a li element and put the innerText value the same as the current item, and append the li at the list.
Javascript
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];
}]
Complete code:
HTML
GeeksforGeeks
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];
}];
Output: