Sublime Forum

\n not working JavaScript

#1

I have a very basic code right here:
let a;
a = ‘hello I am\nA’;
document.write(a);
This code just gives a output of the following:
Hello I am A

It does not give a line break, I pasted the same code in other text editors such as Vsc and atom, they work perfectly there, it just doesn’t work in sublime text!
Any solutions?

0 Likes

#2

i would say st’s is correct but having no idea why other editors output a visual newline. as long as the same codes, editors do not matter.

or consider making your issue more detailed.

0 Likes

#3

document.write writes to the web-page in a browser and thus doesn’t work in node. Sublime Text isn’t a web browser, so I’m not sure what you’re seeing or expecting to see there. If you want to output to the console use console.log.

0 Likes

#4

well this wasnt exactly what i wrote, i just wanted to bring this out, here is what i actually wanted to do:
i wanted to make a multi line variable, lets say my variable is called MultiLine, then i wanted to do this:
let MultiLine; MultiLine = 'this is the first line\nthis is the second line'; document.write(MultiLine);

output should be
this is the first line
this is the second line

whereas is is coming as:
this is the first line this is the second line

0 Likes

#5

im not using node

0 Likes

#6

What are you using then to run that code? Sublime Text doesn’t have a javscript engine.

0 Likes

#7

im just opening the file in my browser, ive linked it to a html file

0 Likes

#8

It looks like you have some misunderstanding about how browser renders HTML. \n will not be a new line after rendering, but a space. <br> is a new line after rendering.

0 Likes

#9

i used “
” too, which works completely fine, i just wanted to know why \n isnt working, my friend showed it to me and it works completely fine with him, he uses atom

0 Likes

#10

with Atom doesn’t make it clearer. Without clearly describe how you guys “output” things, people are hard to explain it clearly as well.

0 Likes

#11

console.log was used

0 Likes

#12

used for what? log what?

0 Likes

#13

console.log is fundamentally different to document.write. HTML documents (which is what you’re writing when calling document.write) don’t care about newlines by default. The console on the other hand does.

0 Likes

#14

console.log('hello\nhello")

0 Likes

#15

that renders nothing.

0 Likes

#16

im so confused… sorry im really new to javascript i started yesterday, it will log it in the console tho wouldnt it

0 Likes

#17

Unless you output something into the web “page” (like by document.write), you are simply living in the JavaScript world (i.e., nothing is really related to HTML). console.log(sometihng) just only shows the variable value of sometihng in the console.

By console, I mean the Console panel which is shown after pressing F12 in your browser.
image

0 Likes

#18

aight tysm

0 Likes

#19

you want to run Javascript from Sublime Text? Use the Quokka plugin.

0 Likes