Print Array Elements Using HTML Table

Here is the Solution of how to print array element in HTML table.

HTML Code:

<p id="demo"></p>

JavaScript Code:

var fruits, text, fLen, i;

fruits = ["Banana", "Orange", "Apple", "Mango"];
fLen = fruits.length;
text = "<table border='1'><tr><th>Index</th><th>Data</th></tr>";
for (i = 0; i < fLen; i++) {
    text +="<tr>"+"<td>"+i+"</td>"+"<td>" + fruits[i] + "</td></tr>";
}
text += "</table>";
document.getElementById("demo").innerHTML = text;

Leave a Reply