How to Create an HTML Table from an Object Array Using JavaScript ? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Tables are a fundamental part of web development, and displaying data in a structured manner is a common requirement. JavaScript provides a powerful way to dynamically generate HTML content, making it easy to create tables from object arrays. Table of Content Using innerHTML propertyUsing appendChild() methodUsing innerHTML propertyThe innerHTML property allows us to set or get the HTML content within an element. This method is concise and straightforward for creating tables. Syntax:selectedElement.innerHTML = "value";Example: The below code implements the innerHTML property to create an HTML table using a JavaScript array. HTML <!DOCTYPE html> <html lang="en"> <body> <script> const data = [ { name: 'Rahul', age: 25, city: 'New Delhi' }, { name: 'Vijay', age: 30, city: 'Muzaffarpur' }, { name: 'Gaurav', age: 22, city: 'Noida' }, ]; function createTableWithInnerHTML() { let tableHTML = '<table border="1"><tr>'; Object.keys(data[0]).forEach(key => { tableHTML += `<th>${key}</th>`; }); tableHTML += '</tr>'; data.forEach(item => { tableHTML += '<tr>'; Object.values(item).forEach(value => { tableHTML += `<td>${value}</td>`; }); tableHTML += '</tr>'; }); tableHTML += '</table>'; document.body.innerHTML += tableHTML; } createTableWithInnerHTML(); </script> </body> </html> Output: Using appendChild() methodThe appendChild() method can be used to dynamically append the created HTML to a selected DOM element by passing the HTML as parameter to it. It appends HTML as child of the selected element. Syntax:selectedElement.appendChild('HTMLToAppend');Example: The below code implements the appendChild() method to dynamically create a HTML table from JavaScript object. HTML <!DOCTYPE html> <html lang="en"> <body> <script> const data = [ { name: 'Rahul', age: 25, city: 'New Delhi' }, { name: 'Vijay', age: 30, city: 'Muzaffarpur' }, { name: 'Gaurav', age: 22, city: 'Noida' }, ]; function createTableWithForEach() { const table = document.createElement('table'); table.setAttribute('border', '1'); const headerRow = document.createElement('tr'); Object.keys(data[0]).forEach(key => { const th = document.createElement('th'); th.appendChild(document.createTextNode(key)); headerRow.appendChild(th); }); table.appendChild(headerRow); data.forEach(item => { const row = document.createElement('tr'); Object.values(item).forEach(value => { const td = document.createElement('td'); td.appendChild(document.createTextNode(value)); row.appendChild(td); }); table.appendChild(row); }); document.body.appendChild(table); } createTableWithForEach(); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Create Array of Objects From Keys & Values of Another Object in JavaScript ? G gopikumarkaushik9065 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League Geeks Premier League 2023 Similar Reads How to create HTML List from JavaScript Array? Creating an HTML list from a JavaScript array allows you to dynamically generate <ul> (unordered) or <ol> (ordered) lists and fill them with data directly from your array. This is useful when you want to display array data in a structured way on your webpage. In this guide, weâll go thro 3 min read How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app 5 min read How to build an HTML table using ReactJS from arrays ? To build an HTML table using ReactJS from arrays we can use the array methods to iterate to iterate the elements and create the table rows Prerequisites:NPM & Node.jsReact JSJavaScript Array.map()HTML TableApproach: To build an HTML table from an array of elements using ReactJS, we can use the a 2 min read How to Create Array of Objects From Keys & Values of Another Object in JavaScript ? Creating an array of objects from the keys and values of another object involves transforming the key-value pairs of the original object into individual objects within an array. Each object in the array represents a key-value pair from the source object. Below approaches can be used to accomplish th 3 min read How to create an object from the given key-value pairs using JavaScript ? In this article, we will learn to create an object from the given key-value pairs using JavaScript.Key-value pair: Unique identifier (key) associated with its corresponding value; fundamental for data organization and retrieval in programming. here we have some common approaches.We will explore the 5 min read How to convert JSON data to a html table using JavaScript/jQuery ? To convert JSON data into an HTML table, there are multiple approaches, each of which has its own strengths. Let's walk through both approaches you mentioned, detailing how each one works.Table of ContentUsing for loopUsing JSON.stringify() MethodApproach 1: Using for loopTake the JSON Object in a v 4 min read Like