Multi-line Strings with Backslashes
What you'll learn: How to use backslashes to continue a string literal across multiple lines in your code without adding line breaks to the actual string value.
The Core Idea
When you write a long string in JavaScript, you might want to break it across multiple lines in your code editor for readability. Using a backslash (\) at the end of a line tells JavaScript: "ignore the line break here and continue this string on the next line." The backslash acts as a line continuation character.
How It Works
When you place a backslash immediately before hitting Enter in a string literal, JavaScript treats the string as if it continues on the next line without actually inserting a newline character into the string's value:
let message = "This is a very long string that \
we want to split across multiple lines \
in our code for readability.";
console.log(message);
// Output: "This is a very long string that we want to split across multiple lines in our code for readability."
Notice that the output is one continuous line. The backslashes removed the line breaks from the code without adding them to the string.
Important Detail
The backslash must be the very last character on the line—no spaces or other characters after it. Even a single space after the backslash will cause a syntax error.
// This works:
let good = "Hello \
World";
// This breaks (space after backslash):
// let bad = "Hello \
// World";
Key Takeaway: A backslash at the end of a line in a string literal continues the string on the next line without adding a newline character—it's purely for formatting your code, not the string's content.