[Solved] How do i change line in JS result ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[Solved] How do i change line in JS result ?

Hello ! solo learners, i got an issue i write some general codes in the below link. I want to know how do i change the line of the results. I already know "\n" is used to creat a new line ( but it doesn't work in variables ) . If you know the solution plz let me know THANK YOU 🍫 https://code.sololearn.com/WXm7B6Wsxbp1/?ref=app

11th Jun 2021, 12:39 PM
Lucifer
Lucifer - avatar
3 Answers
+ 11
document.write(x + "</br>"); document.write(y+"</br>");
11th Jun 2021, 12:41 PM
Simba
Simba - avatar
+ 1
Thanks bro it works !
11th Jun 2021, 1:41 PM
Lucifer
Lucifer - avatar
+ 1
when you console.log(), you output (print) to web browser console, so output is done as expected (with new line char "\n" introducing a new line in output)... conversely, when you document.write(), output is appended to currect web (html) document, and is parsed as rest of html/css/js source code: that's why you require either "<br>" to replace "\n" OR to use style/tag wich do not follow html default behavior for spaces-like characters (treat all including new lines as space, and collapse contiguous ones to only one or zero space -- if at start or end of block content): so another solution could be: document.write("<pre>"+text_including_new_lines+"</pre>"); but you should be aware that's more safe to escape dangerous characters, as in html source code: at least "<" with "&lt;" and "&" with "&amp;" ^^
11th Jun 2021, 5:42 PM
visph
visph - avatar