
JavaScript multi-line String
The JavaScript help create multi-line strings using template literals (introduced in ECMAScript 6). Template literals allow you to define strings that can span across multiple lines without the need for explicit line breaks or concatenation. Here’s an example:
const multiLineString = `This is a
multi-line
string.`;
console.log(multiLineString);
In the above example, the template literal is enclosed within backticks (). The string can span across multiple lines, and line breaks are preserved as part of the string. When you log the multiLineString
variable, it will output the following:
This is a
multi-line
string.
Template literals also support variable interpolation and expression evaluation. You can include variables or expressions within the string using ${}
:
const name = 'John';
const age = 30;
const message = `My name is ${name} and I am ${age} years old.`;
console.log(message);
The message
variable will contain the string “My name is John and I am 30 years old.”.
Template literals provide a concise and convenient way to create multi-line strings in JavaScript, making it easier to work with complex strings or formatted text.