I suggest you to create a js function. The array will be a parameter of the function (this way you can use it with multiple variables). So it will look something like this: function loadTable(names) { for(let i = 0; i < names.length; i++) { let row = table.insertRow(); let date = row.insertCell(0); date.innerHtml = "insert date here"; let name = row.insertCell(1); name.innerHtml = names[i]; } } var array = ["one", "two", "three", "four"]; loadTable(array); //calling the function