tags:

views:

121

answers:

5

“document.write(‘hello world\n’);” and “document.writeln(‘hello world’);” what is the difference between both?

edit

My question is what will be the difference of output.

+1  A: 

afaik there's no difference between them. You'll end up with a line, which has a "new-line-sign" at the end, so the next text youre gonna display will show up in the following line.

if this is a tricky question sorry for my ignorance:)

Katalonis
+5  A: 

The writeln() method is identical to the write() method, with the addition of writing a newline character after each statement.

from w3schools.com

edit: for the sake of completeness :)

writeln on mozilla.org

writeln on w3.org

john_doe
I think he knows that, what he asks is what is the difference with calling writeln with only the line to add vs. calling write and adding newline in the string using `\n` at the end.
awe
@awe: thanks for down-voting. why you think, everyone else gives the same answer on the to's question. what should be the difference in your eye's? the extra work for adding the newline your self? come on...
john_doe
No downvote from me, but w3schools is very far from being any kind of authority on JavaScript.
Tim Down
+1  A: 

document.write() writes to the document without appending a newline on the end. document.writeln() writes to the document appending a newline at the end.

thomasmalt
He knows that, what he asks is what is the difference with calling writeln with only the line to add vs. calling write and adding newline in the string using `\n` at the end.
awe
@awe But isn't the answer obvious? The benefit of using document.writeln() is that you don't need to write \n manually into your strings, and maybe that you don't need to think about platforms using \r\n as a line delimiter, but if the output is a webpage consumed by a browser that isn't an issue anyway.
thomasmalt
+6  A: 

Historically, writeln was intended to handle different newline conventions, of with \n is only one.

There are different conventions for end-of-line. '\n' is the end-of-line marker on UNIX, '\r' on Mac (AFAIK not any more as it's now a UNIX) and '\r\n' is DOS/Windows. Using writeln should automatically use the correct one on the desired platform in other languages, but I don't really know whether JavaScript's document.writeln automatically uses the correct one automagically.

DarkDust
+1  A: 

In theory, writeln() appends a line feed to the end of your string.

In practice, since you're talking Javascript, there's little real difference if you're generating HTML, since HTML ignores extra white space anyway.

Spudley
Read the question carefully: His `write` example also writes a line feed, using the `\n` character at the end of the string.
awe
Spudley: not quite true: whitespace can be significant in HTML. For example, whitespace between two `<img>` elements will introduce a visible gap.
Tim Down
@Tim: yes, hence my use of the word *extra* (as in extra white space) and *little* (as in little real difference). There is, as you say a difference. But in practice, most use of document.write() or writeln() (in my experience) is to produce blocks of text rather than fully formed html, and in that context they probably won't see any difference.
Spudley