Alert new line javascript
A simple one!
In JavaScript, you can create a new line in an alert
box using the following methods:
-
Using the
\n
character:alert("Hello\nWorld!");
This will display "Hello" on the first line and "World!" on the second line.
-
Using the
<br>
tag:alert("Hello<br>World!");
This will also display "Hello" on the first line and "World!" on the second line.
-
Using the
String.prototype.split()
method:alert("Hello World!".split(" ").join("\n"));
This will split the string into an array of words, join them with a newline character, and then display the result.
-
Using the
String.prototype.replace()
method:alert("Hello World!".replace(/ /g, "\n"));
This will replace all spaces in the string with newline characters, and then display the result.
Note that the \n
character is a special character in JavaScript that represents a newline, so you don't need to escape it with a backslash (\
) unless you're using it in a string literal.