How to Fix the "Unterminated String Constant" Error
Learn what causes the unterminated string constant error, how to find the missing or broken quote, and how to escape string text safely without changing its meaning.
"Unterminated string constant" is a syntax error. The parser found an opening quotation mark that starts a string and reached the end of the line, or the end of the file, without finding the matching closing quote. A string constant, also called a string literal, is text written directly in source code between quotes, such as "Hello". Because this is a syntax error, the script usually does not run at all. The line number in the message tells you where the parser gave up, which is often the line where the string began.
Where the message comes from
This exact wording is associated with Microsoft's older scripting engines: JScript, the JavaScript engine in legacy Internet Explorer, and VBScript, used in .vbs files and classic ASP pages. Other languages report the same problem in different words. The causes and fixes in this guide apply to all of them, but the escaping rules differ by language.
- Python 3.10 and later: unterminated string literal (detected at line N)
- Older Python 3 versions: EOL while scanning string literal
- Firefox: SyntaxError: unterminated string literal
- Chrome and other V8-based environments: SyntaxError: Invalid or unexpected token
Why the closing quote goes missing
| Cause | What happens | Typical fix |
| Missing closing quote | The string never ends | Add the closing quote |
| Unescaped quote inside the string | An inner quote closes the string early, and a later quote opens a new string that never closes | Escape the inner quote or use the other quote type |
| Literal line break inside the string | Most single-line string syntaxes cannot contain a raw newline | Replace the line break with an escape sequence such as \n, or concatenate lines |
| Trailing backslash | In C-style languages, " means a literal quote, so the real closing quote is consumed | Write the backslash as \ |
| Curly or "smart" quote used as the closing quote | A typographic quote (U+201C or U+201D) looks like a quote but is not one to the parser | Replace it with a straight ASCII quote (U+0022) |
| Invisible line separator in older JavaScript engines | U+2028 or U+2029 inside a string literal was treated as a line break before ES2019 | Escape it as \u2028 or \u2029 |
| The text </script> inside a string in an inline HTML script | The browser ends the script block at that point, cutting the string in half | Write it as </script> |
Fixing the error step by step
- 1. Note the line number in the error message and open that line. If the line looks correct, check the lines immediately above it, because the string may have started earlier.
- 2. Find the opening quote of the string on that line and identify which character opened it: a double quote, a single quote, or a backtick.
- 3. Scan forward for the matching closing quote of the same type. A syntax-highlighting editor helps here: code after the broken string usually turns the string color.
- 4. Check the string content against the causes in the table above: inner quotes, raw line breaks, a backslash right before the closing quote, curly quotes, and invisible characters.
- 5. Fix the content by escaping the problem characters for your language, switching the outer quote type, or splitting the text into concatenated pieces.
- 6. Save, run the code again, and confirm the error is gone and the string prints the text you intended.
Examples of broken and fixed strings
The snippets below are illustrative examples written for this guide, not captured output from a specific engine.
Missing closing quote (JavaScript)
// Broken
var msg = "Hello, world;
// Fixed
var msg = "Hello, world";
Quote inside the string (JavaScript)
// Broken: the quote before hi closes the string early
var msg = "She said "hi" to me";
// Fixed by escaping the inner quotes
var msg = "She said \"hi\" to me";
// Fixed by using the other quote type
var msg = 'She said "hi" to me';
Line break inside the string (JavaScript)
// Broken
var msg = "Line one
Line two";
// Fixed with an escape sequence
var msg = "Line one\nLine two";
// Fixed with concatenation
var msg = "Line one\n" +
"Line two";
Template literals in backticks can contain raw line breaks in modern JavaScript engines, but legacy JScript does not support them, so use escape sequences or concatenation if the code must run there.
Trailing backslash in a Windows path (JavaScript)
// Broken: \" is read as an escaped quote, so the string never closes
var path = "C:\Temp\";
// Fixed: each literal backslash is doubled
var path = "C:\\Temp\\";
Curly closing quote (JavaScript)
// Broken: the closing character is U+201D (right double quotation mark)
var msg = "Hello”;
// Fixed: the closing character is U+0022 (straight double quote)
var msg = "Hello";
Curly quotes often arrive when code is copied from word processors, chat apps or formatted web pages. They can be hard to see in some fonts, so searching for the characters U+201C, U+201D, U+2018 and U+2019 is more reliable than reading the line.
Quote inside a string (VBScript)
' Broken: the quote before Hello closes the string, and the last quote opens a new one
MsgBox "He said "Hello"
' Fixed: VBScript embeds a quote by doubling it
MsgBox "He said ""Hello"""
VBScript has no backslash escapes. A line break inside a string is written by closing the string and concatenating a constant, for example "Line one" & vbCrLf & "Line two".
Closing script tag inside an inline script (HTML with JavaScript)
<!-- Broken: the browser ends the script block at the inner </script> -->
<script>
var html = "</script>";
</script>
<!-- Fixed: the escaped slash keeps the tag from matching -->
<script>
var html = "<\/script>";
</script>
What a string escaping tool does to your text
A string literal escaping tool takes raw text, such as a message, a file path, a JSON fragment or a block of HTML, and rewrites it so it can be placed between quotes in a specific language without ending the string early. The output represents the same text, but characters that have special meaning inside a string literal are replaced with escape sequences. Most tools also offer the reverse operation, unescaping, which turns an escaped literal back into plain text.
The input should be only the content of the string, not the surrounding code. The output is that content in escaped form, which you then place between the quotes in your source file. The scope matters: escaping rules depend on the target language and on which quote character surrounds the string, so a result escaped for JavaScript is not automatically correct for VBScript, Python, SQL or an HTML attribute. The table below shows common conversions for the two languages that use this error message.
| Character in your text | JavaScript (double-quoted string) | VBScript |
| Double quote " | " | "" |
| Single quote ' | ' (or ' inside a single-quoted string) | ' (no escaping needed) |
| Backslash \ | \ | \ (no escaping needed) |
| Line feed (newline) | \n | " & vbLf & " |
| Carriage return + line feed | \r\n | " & vbCrLf & " |
| Tab | \t | " & vbTab & " |
| Line separator U+2028 | \u2028 | not a special character |
No information about a particular escaping tool was supplied for this guide, so the exact behavior of any given tool cannot be confirmed here. Tools differ in whether they escape non-ASCII characters as \uXXXX sequences, whether they escape single quotes, and whether they add the surrounding quotes for you. Check your tool's description or test it on a short sample before relying on it.
Avoiding unintended changes to your text
- Escape once. Running already-escaped text through an escaper again turns \n into \n, which displays as a literal backslash followed by n instead of a line break.
- Escape for the right context. Text that sits inside a JavaScript string inside an HTML page may need both JavaScript escaping and protection against </script>.
- Match the quote style. If the tool escapes double quotes but your string uses single quotes, inner single quotes will still break it.
- Keep intentional whitespace. Leading spaces, tabs and trailing spaces inside the string are content; make sure the tool or your editor does not trim them.
- Decide deliberately about curly quotes inside the content. Inside a string they are ordinary characters and are valid; only a curly quote used where the closing straight quote should be causes the error.
Verifying the result
- Run the script again and confirm the syntax error no longer appears.
- Print or display the string and compare it with the original text, paying attention to quotes, line breaks and backslashes.
- If you used an escaping tool, unescape its output and compare it with your original input; the two should match exactly.
- For text containing invisible or look-alike characters, compare character codes rather than relying on how the text looks on screen.