New line in template literal
In template literals, you can create a new line using the \n
character. Here's an example:
const templateLiteral = `Hello,
world!`;
console.log(templateLiteral); // Output: "Hello,\nworld!"
In this example, the \n
character is used to create a new line within the template literal.
Alternatively, you can use the template literal
with the raw
tag to create a new line:
const templateLiteral = `${raw`
Hello,
world!
`}`;
console.log(templateLiteral); // Output: "Hello,\nworld!"
The raw
tag is used to indicate that the template literal should be treated as a raw string, without any interpolation or escaping.
You can also use the template literal
with the tagged template literal
syntax to create a new line:
const templateLiteral = taggedTemplateLiteral`
Hello,
world!
`;
console.log(templateLiteral); // Output: "Hello,\nworld!"
The tagged template literal
syntax allows you to specify a tag function that will be called with the template literal as an argument. In this case, the tag function is not specified, so the template literal is treated as a raw string.
Note that in all cases, the \n
character is used to create a new line, and the resulting string will have the new line character included.