4
'use strict'
[1,2,3,4].find(x => x > 1)

When the above code is executed with Node.js 5.0.0, it gives the following error:

TypeError: "use strict"[(((1 , 2) , 3) , 4)].find is not a function
at Object.<anonymous> (C:\src\nodejs\ecma6.js:2:11)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3

The error goes away if I add a semicolon after 'use strict'.

This looks like a bug... Or is there anything deeper - meaning whether there is a list of exceptional cases in the language specification, in which a semicolon is required.

The language specification does list exceptional cases, in which explicit semicolons are required.

2
  • The compiler didn't fail here. It's listed in the language specification in 7.9.1 part 1.
    – slebetman
    Commented Nov 11, 2015 at 5:32
  • This is just an example where JS programmer has decided to drop the semicolon and pretend that it's okay. Javascript statements require using semicolons but it also has autocorrect feature called Automatic Semicolon Insertion (ASI) which some programmers consider to be a feature they're supposed to use. It's like intentionally always speling wrods incorectly and asuming it's fien becayse there's autocorect systme. In this case, dropping the semicolon is not okay because next line starts with a [ which is one of the characters in the JS spec that prevent this autocorrection. Commented Oct 6, 2020 at 6:53

1 Answer 1

8

This is one reason why it is always advised to use semicolons in javascript. The reason it doesn't work is because the code is interpreted as:

"use strict"[1,2,3,4] ...

In other words it's interpreted as:

"use strict"[4] ...

because of the comma operator. This evaluates to the string "s".

Now, the rest of the code is trying to do:

"s".find()

but strings don't have the find method.

To avoid all this, make sure you tell the interpreter that the two lines are separate statements - use a semicolon.


Additional note:

This behavior is required by the ECMAScript standards (at least ES5). In section 7.9.1 part 1 the rules governing this case is defined:

When, as the program is parsed from left to right, a token (called the offending token) is encountered that is not allowed by any production of the grammar, then a semicolon is automatically inserted before the offending token if one or more of the following conditions is true:

  1. The offending token is separated from the previous token by at least one LineTerminator.

  2. The offending token is }.

In this case, the "use strict" and [1,2,3,4]... is parsed. The compiler looks at the resulting statement:

"use strict"[1,2,3,4]...

and notes that this is valid javascript. Therefore a semicolon is not inserted since no "offending token" is found in the statement.

8
  • Never trust semicolon insertion in a language where the rules for semicolon insertion are too complex for humans to keep track. It's OK for languages like go etc. but IMHO not OK for javascript.
    – slebetman
    Commented Nov 11, 2015 at 4:37
  • Because a string's contents can be accessed by index just like an array. "astring"[0] == "a". Likewise, "astring"[0,1,2] == "t".
    – Hypaethral
    Commented Nov 11, 2015 at 4:49
  • @PeterPeiGuo: The compiler didn't fail. The programmer failed to realize that strings can be accessed using foo[bar] syntax.
    – slebetman
    Commented Nov 11, 2015 at 4:52
  • 1
    @PeterPeiGuo: That's the problem about semicolon insertion. In javascript, semicolon insertion works via exception/errors. The interpreter tries to add the next line and if it doesn't make sense syntactically then it's considered as part of the previous statement. If it generates a syntax error then the two lines are separated and parsed again. If it still generates a syntax error then the compiler will output a syntax error otherwise it will continue parsing. This is of course very silly - it makes code perfectly parseable to the compiler but ambiguous to humans.
    – slebetman
    Commented Nov 11, 2015 at 5:27
  • 1
    @PeterPeiGuo: See: ecma-international.org/ecma-262/5.1/#sec-7.9. Specifically 7.9.1 part 1. It describes the mechanism I mentioned above. The compiler is not buggy. It's the programmer's mistake for misunderstanding how the syntax works. To avoid such mistakes I advocate always using semicolons. It's not your fault. It's the fault of the standards.
    – slebetman
    Commented Nov 11, 2015 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.