Breaking strings using Escape sequence:
The escape sequence used to create a new line in Windows and Linux is
\n, but for a few older macs
\r is 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
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"
In both cases, the same output is returned. but as you can see Template Literals make it easier to write multi-line strings.
HTML Break element
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
.innerHTML and not
.innerText as you would with other text content.