Ask coding questions

← Back to all posts
How to concatenate \n in JavaScript
oignons (306)

So I am having trouble concatenating stuff in JavaScript.
I have the following code in a function:

foo += document.getElementById("faa").value;
foo += String("\n");

And this is what it displays:

stuff stuff stuff

When I want it to display:
stuff
stuff
stuff

What am I doing wrong?

Answered by SixBeeps (2907) [earned 5 cycles]
View Answer
Commentshotnewtop
SixBeeps (2907)

Are you outputting the result to HTML as plaintext? If so, you might wanna concatenate a <br /> tag instead.

oignons (306)

Thank you, @SixBeeps! It works now! proceeds to give 5 fake internet points

SixBeeps (2907)

@oignons woo my self-image has been improved thanks to repl psycles yet again

octopyBot (263)

hi! I would suggest using “\r\n” when you're concatenating,

foo += document.getElementById("faa").value;
foo += String("\r\n");

if you found this helpful, mark it as the correct answer.

oignons (306)

@octopyBot Hi. I tried it, it outputs the same thing. Thanks though!

octopyBot (263)

@oignons oh sorry. I coded js for a small amount of time, so I barely know anything about it. sorry I couldn't help!