How to add a newline in JS? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to add a newline in JS?

My code: var num = 10; document.write(num == 8,"\n"); document.write(num === "10"); Why \n is not adding a new line?

2nd Feb 2021, 4:42 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
5 Answers
+ 6
Because you're using document.write it means you output HTML code. You have to use <br/> here as <br/> is a line break in HTML. If you were using console.log, "\n" would work.
2nd Feb 2021, 6:26 AM
Artem 🇺🇦
Artem 🇺🇦 - avatar
+ 11
Use <br> instead of \n
2nd Feb 2021, 5:07 AM
Bartika🎶
Bartika🎶 - avatar
+ 4
Bartika🎶 why? As said in JS course in sololearn, \n means newline! https://www.sololearn.com/learning/1129/
2nd Feb 2021, 5:42 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
+ 4
Faheem You are correct but pls note that document.write() function renders the whole thing in html... so you need to use html tags
2nd Feb 2021, 6:30 AM
Bartika🎶
Bartika🎶 - avatar
+ 2
A new line is added but HTML strips newlines and other forms of whitespace. Remember? The right way to include a line break is using <br> or enclosing in a block element.
2nd Feb 2021, 11:04 AM
Ore
Ore - avatar