-4
<script type="text/javascript">
document.writeln("Hello");
document.writeln("I am new line!");
</script>

Output is:

Hello I am new line!

Code with pre

<pre>
<script type="text/javascript">
document.writeln("Hello World!");
document.writeln("Have a nice day!");
</script>
</pre>

Output is:

Hello
I am a new line!

What's so special about the <pre> tag? Is it only used for line breaks?

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
theking963
  • 2,223
  • 7
  • 28
  • 42

4 Answers4

5

The pre tag works by preserving white-spaces, line feeds etc.

Your javascript is printing Hello. \n I am a new line.

Without the pre tag, HTML ignores the \n. With the pre tag, it honors the \n.

Ayush
  • 41,754
  • 51
  • 164
  • 239
1

Normally, a newline in HTML text is meaningless; text lines are stitched together and flow into paragraphs. The <pre> tag preserves any newlines inside of it. This has nothing to do with Javascript; it's just the nature of HTML.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

Your example doesn't really make sense (note the second example prints "Have a nice day!"), but I think I get the gist of what you're asking.

Yes, the <pre> tag preserves white space. Read the spec:

The pre element represents a block of preformatted text, in which structure is represented by typographic conventions rather than by elements.

http://dev.w3.org/html5/spec/Overview.html#the-pre-element

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0
<pre> tag used to show the values line by line. 
ex: if u put print_r for particular array variable it shows results line by line..
mohan
  • 453
  • 1
  • 5
  • 17