How to Resolve "SyntaxError: Unterminated string constant" Error in JavaScript
The SyntaxError: Unterminated string constant is a common error in JavaScript that occurs when the JavaScript engine is parsing your code and encounters a string literal that was started but never properly closed. This almost always points to a missing quote or an improperly formatted multi-line string.
This guide will explain the common causes of this syntax error and show you how to find and fix them.
The Core Problem: Unclosed String Literals
In JavaScript, a string literal must be enclosed by a matching pair of quotes. You can use single quotes ('...'), double quotes ("..."), or backticks (`...`).
The "Unterminated string constant" error happens when the parser reaches the end of a line (or the end of the file) but is still "inside" a string because it hasn't found the closing quote yet.
Cause 1 (Most Common): Missing a Closing Quote
This is the most frequent cause of the error—a simple typo where you've forgotten to add the final quote to a string.
Example of problem:
// Problem: The string is started with a single quote but never ends with one.
const myMessage = 'This is an example string;
console.log(myMessage);
Error Output:
Uncaught SyntaxError: Unterminated string constant
The parser sees the opening ' and assumes everything that follows is part of the string. When it hits the end of the line, it throws an error because the string was not terminated.
Solution: ensure every opening quote has a corresponding closing quote of the same type.
// Correct: The string is now properly closed with a single quote.
const myMessage = 'This is an example string';
console.log(myMessage);
Cause 2: Incorrect Multi-line Strings
You cannot create a multi-line string using single or double quotes by simply pressing "Enter." This will also result in an "Unterminated string constant" error because the line break terminates the statement prematurely.
Example of problem:
// Problem: You cannot create a multi-line string with single or double quotes this way.
const myText = "This is the first line,
and this is the second line.";
Solution: to create multi-line strings, you must use template literals, which are enclosed in backticks (`).
// Correct: Use backticks for multi-line strings.
const myText = `This is the first line,
and this is the second line.`;
console.log(myText);
Cause 3: Unescaped Quotes Within a String
The error can also occur if your string contains a quote that is the same type as the one used to enclose it, and you have not "escaped" it.
Example of problem:
// Problem: The apostrophe in "it's" prematurely closes the string.
const mySentence = 'It's a beautiful day.';
The parser sees 'It' as a complete string, and then is confused by the s a beautiful day.' that follows.
Solution: you have two options:
- Escape the inner quote: Prefix the inner quote with a backslash (
\).// Correct: The backslash tells the parser that the apostrophe is part of the string.
const mySentence = 'It\'s a beautiful day.'; - Use a different type of outer quote: This is often the more readable solution.
// Correct: Use double quotes to enclose the string so the single quote is not a problem.
const mySentence = "It's a beautiful day.";
How to Debug the Error
- Check the Console: The error message in your browser's developer console will usually give you the line number where the unterminated string begins, which is a strong clue.
- Look at Syntax Highlighting: Your code editor (like VS Code) is your best tool. It will almost always highlight the code in a way that makes the error obvious. The text following the unclosed quote will typically be colored as if it's all part of the string.
Conclusion
The SyntaxError: Unterminated string constant is always caused by an improperly formatted string literal.
To solve it, check your code for:
- A missing closing quote (
'or") or backtick (`). - A multi-line string that is not enclosed in backticks.
- An unescaped quote inside a string that is of the same type as the enclosing quotes.
Using a modern code editor with a good linter will help you catch and prevent these common syntax errors before you even run your code.