Categories
JavaScript Answers

How to Fix the ‘SyntaxError: unterminated string literal’ Error in Our JavaScript App?

Spread the love

Sometimes, we may run into the ‘SyntaxError: unterminated string literal’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: unterminated string literal’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: unterminated string literal’ When Developing JavaScript Apps

To fix the ‘SyntaxError: unterminated string literal’ when we’re developing JavaScript apps, we should make sure that enclose our string content with single quotes, double quotes or backticks.

On Edge, the error message for this error is SyntaxError: Unterminated string constant.

And on Firefox, the error message for this error is SyntaxError: unterminated string literal.

For instance, if we’re trying to split a long string into multiple lines, we can’t write:

const longString = 'This is a very long string which needs
                  to wrap across multiple lines because
                  otherwise my code is unreadable.';

Instead, we write:

const longString = 'This is a very long string which needs \
                  to wrap across multiple lines because \
                  otherwise my code is unreadable.';

to add a backslash to separate a long string into multiple lines.

Conclusion

To fix the ‘SyntaxError: unterminated string literal’ when we’re developing JavaScript apps, we should make sure that enclose our string content with single quotes, double quotes or backticks.

On Edge, the error message for this error is SyntaxError: Unterminated string constant.

And on Firefox, the error message for this error is SyntaxError: unterminated string literal.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *