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>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<button onclick="showAlert()">
click here
</button>
<script>
var text =
"This is line 1\nThis is line 2";
function showAlert() {
alert(text);
}
</script>
</body>
</html>
Which will produce the below result.
That's it for today. I hope you've enjoyed this tutorial. Thanks for reading.