JavaScript SyntaxError: Unterminated string literal

This JavaScript error unterminated string literal occurs if there is string which is not terminated properly. String literals must be enclosed by single (‘) or double (“) quotes.

Message:

SyntaxError: Unterminated string constant (Edge)
SyntaxError: unterminated string literal (Firefox)

Error Type:

SyntaxError

What happened?

There is  a string in the code which is not terminated. String literals needs to be enclosed by single (‘) or double (“) quotes. JavaScript sees no difference between single-quoted strings and double-quoted strings.



Example 1: In this example, the single quotes are used and the string is not terminated, So the error has occurred.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
<title>Syntax Error</title>
</head>
<body>
    <script>
    var GFG_str = 'This
                     is 
                     GeeksForGeeks';
    document.write(GFG_str);
    </script>
</body>
</html>

chevron_right


Output(In console):

SyntaxError: Unterminated string constant

Example 2: In this example, the double quotes are used and the string is not terminated, So the error has occurred.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
<title>Syntax Error</title>
</head>
<body>
    <script>
    var GFG_str = "This
                     is 
                     GeeksForGeeks";
    document.write(GFG_str);
    </script>
</body>
</html>

chevron_right


Output(In console):

SyntaxError: Unterminated string constant

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.