In JavaScript, you can create multiline strings using template literals (also known as template strings) which are enclosed by backticks () instead of single or double quotes. Inside the template literal, you can use the newline character (\n`) to create line breaks. For example:

const multilineString = `This is a multiline string.`;
console.log(multilineString);

You can also use the ‘+’ operator to concatenate strings on multiple lines, like this:

const multilineString = "This is a " + "multiline " + "string.";
console.log(multilineString);

You could also use ‘\’ at the end of the line, like this:

const multilineString = "This is a \ multiline \ string.";
console.log(multilineString);

Keep in mind that in this last example, it will be treated as one line, so if you want to debug the string, you will see it in one line, but when you print it, it will respect the line breaks.

Also Read:

Categorized in: