Since you're outputting to an HTML document, \n
doesn't mean newline, it's just whitespace. In HTML, all whitespace is just a single space in the rendered output.
To output a line break, you ouput a br
element:
document.write("<br>");
Note that while document.write
is fine for small experiments and such, it has very little place in actual web development. Using document.write
adds to the document where the script
tag is during the main parsing of the document, but once the main parsing is done, the document is closed. If you use document.write
again (for instance, in response to an event), it will implicitly do a document.open
, wiping out all of the document contents, and then write to the newly-opened document.
But again, it's fine if you're just running some code for experiments and quick learning, just beware of what it actually does. As you progress, you'll start using the DOM instead (either directly, or through a DOM library like jQuery).
` not `\n` – dmeglio Sep 03 '15 at 15:43