Javascript Split on Newline
In this article, we will see how to split on newline Javascript.
Using break tag
document.write(" Hello World! <br/> Hello World!");
/*
Output:
Hello World!
Hello World!
*/
In the above JS code, we have some text inside the document.write() function for printing. Here, I want to split the word, we cannot use ‘\n’ comment because the newline comment is working only console.log() funciton. So, we can use HTML break tag <br/>. You know that the break tag breaks or split the line. See the above output the word is successfully split. Similarly, we can perform breaking the line in many ways.
document.write(" Hello World!")
document.write(" Hello World!")
/*
Output:
Hello World!Hello World!
*/
In the above code, we’ve printed “hello world” twice using document.write() method. But it displayed on the same line because javascript codes are executed top to down one statement at a time like this. So, when you say document.write() it will display then, again document.write() so, that will e getting displayed beside the previous word. Because we’ve been told by the browser that was to break the line, to do that you have to use the <br/> tag. So, we use <br/> break tag to split on newline in javascript.
Newline ‘\n’ Comment
console.log("Hello World! \n Hello World!");
Here, we can use ‘\n’ newline comment to split the line. But this newline comment works only in double quotes and only for console.log method.