Display each array on a new line


Hi I’m learning Javascript, but can’t seem to find a solution to display each array on a new line, I’ve tried
and \n but these don’t work. Please help

function start() { 
document.getElementById("task").innerHTML="Task 1"; 
var arrNames = ["John", "Peter", "Phil", "Mark"];
var vindex = 0;
var vOutput = "";
vOutput = "List of names: "+arrNames+"<br/>";


window.onload = start;


if we look at the output:

vOutput = "List of names: "+arrNames+"<br/>";

we can see you inserted the array directly into the string, if you want to do what you want, you would need a loop so could insert each name individually as well as a break statement


Thank you, I know how to create a loop, I will try that.


yes, because now you just inserted the whole array in one go, and only after the array you inserted a break statement, that won’t work i am afraid.

Also, DOM manipulation is expensive, try to do as much as manipulation as possible in a single go


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.