document.write vs document.writeln

Hi
I can't see any difference between document.write and document.writeln. I searched the internet and found the followng example if I use the <pre> it works fine if I remove the <pre> it does not work. so what is this <pre>
Why write and writeln does not work without it?
What pre is used for?
Thanks

<html>
<body>

<pre>
<script type="text/javascript">
document.write("line1");
document.writeln("line2");
document.write("line3");
</script>
</pre>
</body>
</html>
jean11Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sjklein42Commented:
writeln adds a "newline" character to the end of its output.  write does not.

<pre> causes the output to be shown "literally".  It is meant for what they call "preformatted" text and usually is displayed in a fixed font like Courier rather than a proportional font.

Within a <pre> block, newline characters are preserved.  Outside a <pre> block, newlines are generally ignored and you need a "<br>" tag to start a new line.

So you can either use "writeln" for all your output, or you need <pre>, or you need to insert <br> tags between each line.

Hope that helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jean11Author Commented:
Thanks for the reply
Everything was clear before the follwing statement:
>So you can either use "writeln" for all your output, or you need <pre>, or you need to insert <br> tags between each line.

The above statement was confusing
writeln alone as you confirmed (without <pre> will not work. Right?
<pre> alone will not insert a new line? correct

sjklein42Commented:
Even if you use writeln you will also need <pre>.  Your choice is between the use of a <pre> block plus writeln, vs the use of <br> tags between each line.  They're both pretty much the same but the choice may depends on whether you want fixed-width font (with <pre>) or variable.
CCSOFlagCommented:
jean,

the difference is <pre> pays attention to white space and carriage returns in the text.  Normally text on an html page ignores multiple spaces and carriage returns.  writeln adds a carriage return not a <br /> tag.  Thus if you do not have the <pre> around it the carriage return will be removed which makes it just like a write statement.  On the flip side, if you use write in a <pre> tag it will just be one continuous line because write does not input carriage returns.  writeln is mainly used for writing to files rather than writing to the screen.
jean11Author Commented:
thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.