- String concatenation and new line characters (
- Using backslash in string
- Using Template string literals (Recommended)
Let’s check each method with an example.
To create a multiline we can use the new line character (
n ) in our string. It is used to span our string across multiple lines.
Now, if we have multiple strings that you want to span across multiple lines then we can use the new line character
n for line break alone with the concatenation operator (
) in the string.
const str = "This is n multi-line n String." console.log(str)
The backslash (
) placed at the end of the
This is multi-line String
The above two methods were used before the introduction of ES6. In ES5, we had to manually insert new line use (
Template literals are defined as literals that are delimited with backticks characters (
`). It is used for multi-line strings or for string interpolation with expressions.
We can create a new line using the Enter key inside the template literal.
const str = `This is a multi-line string using template literals.` console.log(str)
This is a multi-line string using template literals.
Here, we just have to write our string inside the backticks (
``) and hit enter for a new line.
Read more about Template String Literals and String Interpolations.