JavaScript unterminated string literal Error (Resolved)

JavaScript syntax error “unterminated string literal” occurs due to any of the following reasons –

  1. Lack of quotation mark at the end of the string.
  2. Extra quotation mark before the terminator.
  3. Failure to escape the internal quotation mark.
  4. Splitting the string into multiple lines without using the line continuation character.

Let’s have a look at each of these reasons in detail with the help of examples.

unterminated string literal error in javascript

Reason 1: Lack Of Quotation Mark At The End Of The String

This is the most common reason for this error. It occurs when we forget to add a quotation mark at the end of the string.

For example,

var str = "This is a string" // Correct

var str = "This is a string // Wrong

As you can see in the above code, the second string is missing the quotation mark at the end which is resulting in an error.

Reason 2: Extra Quotation Mark Before The Terminator

This error occurs when there is an extra quotation mark before the string terminator. It might happen due to a typo while writing the code.

For example,

var str = "This is a string" // Correct

var str = "This is a string"" // Wrong

As you can see in the above code, there are two quotation marks before the terminator which is resulting in an error.

Reason 3: Failure To Escape The Internal Quotation Mark

This error occurs when we forget to escape the internal quotation mark in the string. In order to escape a character in JavaScript, we use backslash (\).

For example,

var str = "This is a \"string\""; // Correct

var str = "This is a "string""; // Wrong

As you can see in the above code, the internal quotation mark is not escaped which is resulting in an error.

Reason 4: Splitting The String Into Multiple Lines Without Using The Line Continuation Character

This is a very common error that can occur while writing the code. In JavaScript, we can split the string into multiple lines by using the line continuation character (\).

For example,

var str = "This is a \
string"; // Correct

var str = "This is a
string"; // Wrong

As you can see in the above code, the string is split into multiple lines without using the line continuation character which is resulting in an error.

You can even use a template literal to split the string into multiple lines.

For example,

var str = `This is a
string`; // Correct

As you can see in the above code, we are using a template literal (denoted by `) to split the string into multiple lines. This is a new feature in JavaScript and it doesn’t require the use of the line continuation character.

It’s best to use an online JavaScript editor to avoid these kinds of errors. You can also use a linter tool to find these errors in your code.

Conclusion – JavaScript unterminated string literal Error

In this article, we saw what is a JavaScript syntax error “unterminated string literal” and how to fix it. This error is quite common and can be easily fixed once you know the reasons behind it. I hope this article was helpful and that you were able to learn something new from it.

If you have any questions, please feel free to post them in the comments section below.

Happy coding!

Leave a Reply