How to add a new line in the alert box ?

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

Example-1: This example adds \n between the 2 lines to the text of alert box.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript |
      New line in alert box.
    </title>
</head>
  
<body style="text-align:center;"
      id="body">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p>
        Click on the button to pop 
      alert box with added new line.
    </p>
    <button onclick="gfg_Run()">
        click here
    </button>
    <script>
        var text =
            "This is line 1\nThis is line 2";
  
        function gfg_Run() {
            alert(text);
        }
    </script>
</body>
  
</html>

chevron_right


Output:



  • Before clicking on the button:
  • After clicking on the button:

Example-2: This example adds \n among the 3 lines to the text of alert box.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript 
      | New line in alert box.
    </title>
</head>
  
<body style="text-align:center;" 
      id="body">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p>
        Click on the button to pop
      alert box with added new line.
    </p>
    <button onclick="gfg_Run()">
        click here
    </button>
    <script>
        var text = 
            "This is first line\nThis is "+
            "middle line\nThis is last line";
  
        function gfg_Run() {
            alert(text);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.