Javascript line break in HTML | br Tag | Alert box “\n”

Break a line in JavaScript can do by using a “br” tag. If you have 2 or more a string then add br¬†enclosed between 2 brackets < > enclosed in double quotation marks, and preceded and followed by the + sign:

+"<br>"+

USE JS to insert a line break into HTML?

The example of concatenated 2 strings, display in one <p> tag. To break line using a br tag.

<!DOCTYPE html> 
<html> 
<body> 
	<p id="c1"></p> 
	<p id="c2"></p>
<script> 
  
 
   // strings
    var str1 = "EyeHunts";
    var str2 = "Next Line"
    
    document.getElementById("c1").innerHTML = str1 + "<br>" + str2; 
  
</script> 
  
</body> 
</html> 

Output: EyeHunts
Next Line

Js break line in Alert box

In the alert box, you can use “\n”, to break the sentence into a new line.

<!DOCTYPE html> 
<html> 
<script type="text/javascript">
    alert("Hello there.\nI am on a second line ;-)")
</script>
<body> 
  
</body> 
</html> 

Output:

Js break line in Alert box

If you want a linebreak in actual javascript, use the \n escape sequence.

Do comment if you knew another way to break the sentence in a new line using JavaScript code or you have any doubts.

Note: The All JS Examples codes are tested on Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.