Display each array on a new line


#1

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/>";

document.getElementById("output").innerHTML=vOutput;
}

window.onload = start;

#2

if we look at the output:

vOutput = "List of names: "+arrNames+"<br/>";
console.log(vOutput)

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


#3

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


#4

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


#5

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