Thursday, September 4, 2025
HomeLanguagesJavascriptHow to add a new line in the alert box ?

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

html




<h1 style="color:green;">
    neveropen
</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>


Output:

How to add a new line in the alert box ?

How to add a new line in the alert box ?

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

html




<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>


Output:

How to add a new line in the alert box ?

How to add a new line in the alert box ?

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS