How to Assign a Multiline String to a Variable in JavaScript: A Beginner's Guide
\n\nHave you ever written a long text in code and thought: "Can't I just press Enter and continue on a new line?" If you're new to JavaScript, you've probably already encountered this. You try to save a multiline text into a variable, but you get a syntax error. Sound familiar?
\n\nThe question "How can I assign a multiline string literal to a variable?" is one of the most popular on Stack Overflow. Thousands of developers are looking for an answer. And it's no surprise: working with multiline strings is a fundamental skill that everyone writing JavaScript needs to master.
\n\nIn this article, we won't just give a dry answer. We'll break down three working methods, show real-life examples, and suggest which method to choose so your code is clean, readable, and doesn't cause headaches.
\n\nWhy Doesn't JavaScript Like Line Breaks in Quotes?
\n\nLet's get straight to the main problem. If you try to write like this:
\n\nlet message = "Hello!\nHow are you?\nEverything okay?";\nconsole.log(message);\n\n…you'll get an error SyntaxError: unterminated string literal. Why? Because in regular strings (in double or single quotes), JavaScript assumes the string ends on the same line it started. A physical line break breaks the string.
\n\nBut don't be upset. We have at least three ways to get around this limitation. And the first one is the most modern and convenient.
\n\nMethod 1: Template Literals — Your New Best Friend
\n\nStarting with ES6 (ECMAScript 2015), JavaScript introduced template literals. These are strings enclosed in backticks (` `). And they solve the multiline problem once and for all.
\n\nSee how simple it is:
\n\nlet multilineString = `This is the first line.\nThis is the second line.\nAnd this is the third line.`;\n\nconsole.log(multilineString);\n// Output:\n// This is the first line.\n// This is the second line.\n// And this is the third line.\n\nIt works out of the box. You just write text with line breaks, and JavaScript preserves them as is. No extra characters, no magic.
\n\nWhy is this the best option for beginners?
\n- \n
- Readability: The code looks exactly as you intended. \n
- Interpolation support: You can insert variables directly into the string using
${...}. \n - Standard: All modern browsers and Node.js support template literals. \n
Example with interpolation:
\n\nlet name = "Anna";\nlet greeting = `Hello, ${name}!\nWelcome to PythonLib.\nToday is a great day to learn coding.`;\n\nconsole.log(greeting);\n// Output:\n// Hello, Anna!\n// Welcome to PythonLib.\n// Today is a great day to learn coding.\n\nThis method is so convenient that it's used in 90% of cases in modern development. If you're just starting out, remember this one.
\n\nMethod 2: String Concatenation (Old but Reliable Method)
\n\nBefore template literals, developers used concatenation — joining strings with the + operator. This method is still relevant, especially if you're working with very old code or cannot use ES6 for some reason.
Here's what it looks like:
\n\nlet multilineString = "This is the first line.\\n" +\n "This is the second line.\\n" +\n "And this is the third line.";\n\nconsole.log(multilineString);\n// Output:\n// This is the first line.\n// This is the second line.\n// And this is the third line.\n\nNotice the \\n — this is an escape sequence
Blogs
Book Recommendations
Read also
Go complains about go.mod? Why this happens and how it works
Creating a Telegram bot in Rust: a step-by-step guide for the bold
HTTP client in Go: making requests correctly from A to Z
How Swift Distinguishes a Simulator from a Real Device: A Complete Breakdown
Mutation Testing in C#: Breaking Code to Make It Better