-1

I am practicing javascript using google chrome developer console. As per my knowledge we use \ to write in multiple lines on document with Javascript. I try to write

document.write("Hello world \ How are you?");

But it simply displays "Hello world How are you". I was expecting Hello world and How are you in different lines.

Ganesh Devkate
  • 109
  • 1
  • 13

2 Answers2

0


the browser uses HTML to display stuff.
In HTML you make a new line using <br/> (or <br> or </br>) for "break row".
If you want to break it into two lines, use this tag instead of \n.
Greets!

Syntac
  • 1,687
  • 11
  • 10
  • @GaneshDev So here is the thing: If you write `\n` in js, it would result in a linebreak in that string indeed. So if you want to print it out to the console, you can write `console.log("Hello \nWorld")` and it indeed would result in two separate lines. If you use document.write("Hello \nWorld") it indeed would result in a linebreak inside the source-code (!). You can check that by pressing Ctrl+U and looking it up in the source code. However, the browser will ingnore this, as he only interprets markup-tags. So you need to use `
    ` if you want a linebreak on your webpage. Greets!
    – Syntac Oct 08 '16 at 05:27
  • Yes you are correct. I was searching for pure javascript for multiple lines without using html. – Ganesh Devkate Oct 08 '16 at 05:43
  • @GaneshDev So hope you got your answer now? Or still any questions? – Syntac Oct 08 '16 at 05:44
0

You can write New Line in JavaScript by <br> if you will Run it in chrome Console ,

document.write("Hello world <br> How are you?"); 

but check it to know more for New-Line in JavaScript

Community
  • 1
  • 1
Mohammed Magdy
  • 114
  • 1
  • 7