JavaScript Add String Newline Character


JavaScript Add String Newline Character : Sometimes we need to add the newline character using JavaScript. It is very easy to add newline character using JavaScript.Here in this tutorial we are going to explain how you can add the newline charcter using JavaScript. You can use our online editor to try and edit the code online.


JavaScript Add String Newline Character Example

You add break line in JavaScript as below –

JavaScript Add String Newline Character Example:

 
<html>
<head>
<script type="text/javascript">
function newlineExample(){
var testArray = ["Banana", "Mango","Grapes", "papaya"];
for(i=0; i<testArray.length; i++){
   document.getElementById("output").innerHTML += testArray[i]+"<br>";
}

}
</script>
</head>
<body>
<a href="#" onclick='newlineExample()'>Print Array Using Break Line</a>
Output = 

<p id="output"> </p>
</body>
</html>                                                                                                                                            

Try it »

br tag is used to add the break line between the array elements. The above function will add breakline in after each element of array. It adds the br tag after the array element & appends it to the paragraph with id “output”. It will print array elements line wise. On the same way you can add the break line anywhere you need.

If you run the above example it will produce the output something like this –

JavaScript Add String Newline Character


Advertisements

Add Comment

📖 Read More