Skip to content
Advertisement

Why is “” invalid in JavaScript?

While writing "" in Chrome console I get the following error:

VM242674:1 Uncaught SyntaxError: Invalid or unexpected token

In Firefox it gives following error:

Uncaught SyntaxError: '' string literal contains an unescaped line break

while writting "\" gives: "\" in both browsers

What is the proper way to write "" in JavaScript?

Advertisement

Answer

Do not let the rendering of a string in the console, which may display strings including escape sequences and wrapped in quotes because it is a debugging tool, confuse you.

If you want a slash in a string, then escape it with a second slash.

const string = "\";
const node = document.createTextNode(string);
document.body.appendChild(node);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement