Create new line in javascript
In JavaScript, you can create a new line using the following methods:
-
Using the
\n
character:var myString = "Hello\nWorld!";
This will create a new line in the string.
-
Using the
<br>
tag:var myString = "Hello<br>World!";
This will create a new line in the string, but it's not recommended to use HTML tags in JavaScript strings.
-
Using the
String.prototype.split()
method:var myString = "Hello World!"; var newLine = myString.split(" ")[1];
This will split the string into an array of words, and then extract the second element of the array, which will be the new line.
-
Using the
String.prototype.replace()
method:var myString = "Hello World!"; var newLine = myString.replace(/ /g, "\n");
This will replace all spaces in the string with new lines.
-
Using the
String.prototype.concat()
method:var myString = "Hello"; var newLine = myString.concat("\n", "World!");
This will concatenate the string with a new line and another string.
Note that in JavaScript, strings are immutable, so you can't simply add a new line to a string. You need to create a new string with the new line.