JavaScript Add Line Breaks to Javascript Alert Box

Hello devs, today I'll show you how to add line breaks into javascript alert box. We'll see an easy and short example of how we'll achieve our goal. So, let's see an example below.


We'll use backslash n (\n) to add a new line to the content of alert box. Let's see the below source code.

html:<!DOCTYPE html>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <link rel="stylesheet" href="">
     <button onclick="showAlert()"> 
        click here 
        var text = 
            "This is line 1\nThis is line 2"; 
        function showAlert() { 

Which will produce the below result.

That's it for today. I hope you've enjoyed this tutorial. Thanks for reading.