Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

What is the difference between document.write(‘hello world\n’); and document.writeln(‘hello world’);?

Edit

My question is what will be the difference of output.

like image 533
Jitendra Vyas Avatar asked Sep 24 '10 10:09

Jitendra Vyas


People also ask

What is the difference between document writeln and document write?

The Difference Between write() and writeln() writeln() adds a newline character after each statement. write() does not.

What is pre tag Explain the difference between write () and writeln ()?

write and writeln are the same function. The only difference is that writeln adds a new line at the end of the text.


5 Answers

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.

like image 188
DarkDust Avatar answered Nov 08 '22 20:11

DarkDust


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

like image 26
Andreas Avatar answered Nov 08 '22 18:11

Andreas


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.

like image 38
Spudley Avatar answered Nov 08 '22 20:11

Spudley


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:)

like image 36
Katalonis Avatar answered Nov 08 '22 19:11

Katalonis


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.

like image 38
thomasmalt Avatar answered Nov 08 '22 18:11

thomasmalt