1

Chrome version: Version 57.0.2987.110 (64-bit)

This is the error i get: "Uncaught SyntaxError: Invalid or unexpected token".

This is the code:

var x = "\ 
";

If i remove the space behind the backslash, everything works fine.

Why does this happen?

tedi
  • 6,350
  • 5
  • 52
  • 67
  • if you remove the space afer the backslash, the backslash escapes the line break and therefore makes this valid JS, but the escaped line breaks are gone for good. If you want the String to contain a line break you'll have to add one with `\n`. Wich often lead to this pattern `\n\\` . multiline strings were always nasty in JS before [template-strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) were introduced. – Thomas Mar 21 '17 at 14:27

2 Answers2

4

Because its an unterminated string literal.

The JavaScript parser takes a backslash at the end of an unclosed quotation as a line continuation of the string. But you can have backslashes inside strings, so if the backslash isn't the last character of the line, how is the parser supposed to infer that it means a line continuation?

For example:

// this is legit
var foo = "the path is C:\\Users\\bob";

// this too
var foo = "the path is \
C:\\Users\\bob";

// this is an error: unclosed quote, no continuation
var foo = "the path is C:\\Us
ers\\bob";

// your error case, altered slightly to clarify
var foo = "the path is\ C
:\\Users\\bob";

In the error cases the parser can't tell that backslash isn't part of a file path and was meant as a line continuation: it doesn't know what a file path is. It has no a priori knowledge of language, it has to be able to work with arbitrary sequences of characters. The fact that its whitespace doesn't matter, there's just no way for the parser to infer you meant it to continue the string on to the next line.

This is a frequent source of errors because you can't tell what the problem is by looking at the source code. As a less error prone alternative, you may use any of the following:

// concatenation
var foo = "the path is " +
  "C:\\Users\\bob";

// Array.prototype.join
var foo = [
  "the path is ",
  "C:\\Users\\bob"
].join("");

// ES 6 template strings, note that whitespace is preserved
let foo = `
  the path is
  C:\\Users\\bob
`;
Jared Smith
  • 19,721
  • 5
  • 45
  • 83
2

The \ character is used to convert special characters to literals.

For example \t will be converted to a tab space. There's a reference on special characters at the end of paragraph 8.4 of the ECMAScript specs.

As the note at the mentioned spec says, there's one notable exception: the newline character. It is not converted to its literal form and is instead essentially skipped. Think of it as the escape sequence for an empty string.

"abc\ def" === "abcdef"

This has the interesting side effect of letting you "format" your strings over multiple lines, purely for code aesthetics purposes.


In your specific case

var x = "\ ";

will create a string with a space followed by a literal newline character, which is not a valid syntax.

var x = "\ ";

will escape the newline and create an empty string, which is valid.

alebianco
  • 2,475
  • 18
  • 25