- Breaking string using an Escape sequence
- New line using Template literals
- HTML Break Element
- Other Related Concepts
The escape sequence used to create a new line in Windows and Linux is
\n, but for a few older macs
\ris used. The implementation of escape sequences is quite straightforward.
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"
New Line using Template Literals:Template literals sound quite fancy, but underneath the jargon, they are just string literals that allow embedded expressions.
They make it easier to use multi-line strings. Template literals are enclosed within the backtick
In both cases, the same output is returned. but as you can see Template Literals make it easier to write multi-line strings.
let flexiple = "Hire the \ntop 1% \nfreelance talent"; let newstring = `Hire the top 1% freelance talent`; console.log(flexiple); //Output: "Hire the //top 1% //freelance talent" console.log(newstring); //Output: "Hire the //top 1% //freelance talent"
Note that break elements must be used only where the division of a line needs to be significant. But since this method is quite common we look at it as well.
<html> <body> <p id="newline"></p> <script> let flexiple = "Hire the" + "<br>" + "top 1% "+ "<br>" + "freelance talent"; document.getElementById("newline").innerHTML = flexiple; </script> </body> </html>
Note: Remember to use the
.innerTextas you would with other text content.