How to add new line in string?

How to add new line in string?


Here is code:

I’m trying to achieve this:


You can add a new line in a string with "\n"


take a look at jsfiddle


I stand corrected, go to the demo you have and inspect the elements. The new lines are there, the problem is how you put them in the DOM.


Since you use innerHTML you can use HTML < br /> for line breaks. If you want a line break in JS (for example if you use window.alert()) you use “\n”.

EDIT: Have a look at this stackoverflow question.


I want to use \n for new line like i did on line 5.
In another case if i print “test\n\test” it goes to “test test”, i open dev tools and saw there is new line but how can i achieve this also in to be printed in “preview” of html, with new lines? i want to make list by adding "- " like prefix and “\n” like suffix so it goes to new line


By using < br /> it will continue on a new line. Or you can wrap your final customItems in between < pre > tags: document.getElementById("ovde").innerHTML = "<pre>" + customItems + "</pre>";


here: [ ]
it doesn’t work with "<pre>"
line 343, how can i make it so down below when i set items to div to get something like:
test1, test2, test3



EDIT: if u use dev tools, just click on blue button play if debbuger shows


That is strange, the code seems ok to me :confused:


The code does what it is supposed to do, the problem is that he tried to manipulate a string but did not return a string, instead made it an html element. The problem is with the html element he needs to find the right way, I’m not sure if he has tried the <br> or not but it was mentioned already.


This is what me and @BenGitter tried to get you to come up with:


That should normally work, but @nameToReachPeople is using React. I have no experience with React, so I won’t be able to help.


Well don’t feel bad, we answered the original question at least.

For the react part he should add it dynamically as in append it from the loop, but that is out of the scope of this question and I think should go into the react help category.

Take a look at for learning react. I’m about to go dig in myself.

The link was from


Thanks all for help, sorry i didn’t mention it is react js, i was thinking it doesn’t matter


I achieve this with