. Advertisement .
..3..
. Advertisement .
..4..
How many methods can we use to add a new line in Javascript? The answer is at least four. If you struggle with this task, turn to ITtutoria guidelines for detailed instructions on each method.
How to Add A New Line in Javascript?
Method 1: Use \n Symbol
One effective method to add some new lines while printing certain words to your Javascript console is to turn to the \n symbol. It stands for “new line”.
This illustration will make things simpler for your comprehension:
let exp_str = "Hello! Welcome to Ittutoria.net\nHere is the method to add a new line in JavaScript";
console.log(exp_str);
Your output will be like this:
Hello! Welcome to Ittutoria.net
This is the method to add a new line in JavaScript
Another strategy is to use “Enter”. It is particularly effective when adding some new line breaks with a certain template syntax, described as follows:
let exp_str1 = `Hello! Welcome to Ittutoria.net!
Here is the method to add a new line in JavaScript
Using "Enter" to add the new line`;
console.log(exp_str1);
Output:
Hello! Welcome to Ittutoria.net!
Here is the method to add a new line in JavaScript
Using "Enter" to add the new line
Method 2: Use Tempalete Literals
Template literals (also known as template strings) have been introduced for many years already. These functions make it even easier for you to insert new string lines.
So what are template literals exactly? They are merely strings coiled in backticks (‘) instead of singles (‘) or double marks (”). One special attribute about them is the preservation of white space, newlines included:
const exp_String = `Add a new line in JavaScript
use Tempalete Literals`;
console.log(exp_String);
Output:
Add a new line in JavaScript
use Tempalete Literals
Method 3: Use the \r Escape Sequences
Many programmers choose to count on the Escape sequences for new line creations in Javascript. One such sequence is \n, which is already introduced above. For a few computers, \r works much better. Its implementation is fairly uncomplicated and easy to learn!
let flexiple = "Hire the top 1% freelance talent";
let newstring = "Hire the \ntop 1% \nfreelance talent";
console.log(flexiple);
//Output: "Hire the top 1% freelance talent"
console.log(newstring);
//Output: "Hire the
//top 1%
//freelance talent"
Remember not to add more spaces after the sequence. Javascript would deem them as space and move them to your output.
Method 4: Use HTML Break
You can also add HTML breaks to the string and add new Javascript lines as a result.
Here is one thing to note, however. These breaks are only effective where the line divisions require significance.
<!DOCTYPE html>
<html>
<body>
<p id="addnewline"></p>
<script>
let fixed_str = "Add a new line " + "<br>" + "in JavaScript "+ "<br>" + "Use HTML Break";
document.getElementById("addnewline").innerHTML = fixed_str;
</script>
</body>
</html>
Output:
Add a new line
in JavaScript
Use HTML Break
In this example, we suggest you employ the “.innerHTML” instead of “.innerText” like other textual content.
Conclusion
This article has shown you four methods to add a new line in Javascript. Each method has distinct advantages and suits different circumstances, so assess your situation to pick the optimal solution!Once adding new lines, you might want to print out the array. Check this guide for more info on this task. For more instructions or support, feel free to contact the ITtutoria team.
Leave a comment