How to Line Breaks to JavaScript Alert?

04-Nov-2020

.

Admin

Hi Guys,

In this example,I will learn you how to line breaks to javascript alert box.you can easy and simply create to line breaks to javascript alert box.

To add a new line to the content of alert box we are going to use \n backslash(n).

Example:


<!DOCTYPE HTML>

<html>

<head>

<title>

JavaScript |

New line in alert box.

</title>

</head>

<body style="text-align:center;"

id="body">

<h1 style="color:#af1bf9;">

NiceSnippets

</h1>

<p>

Click on the button to pop

alert box with added new line.

</p>

<button onclick="ns_Run()">

click here

</button>

<script>

var text =

"This is line 1\nThis is line 2";

function ns_Run() {

alert(text);

}

</script>

</body>

</html>

It will help you...

#JavaScript