0

I have this code in Node 7.8.0:

function myfunction(n) {
   n = 3
   [].forEach(n => "do something")    
}

It returns:

   [].forEach(v => "do something")    
    ^
SyntaxError: Unexpected token ]

But if I add something before the brackets like a semicolon or a variable declaration it runs without errors. Just like this:

function myfunction(n) {
   n = 3;
   [].forEach(n => "do something")    
}

I am so confused about this.

Carlos Govea
  • 1
  • 1
  • 1
  • 1
  • 2
    `n = 3[].forEach(n => "do something")` Does that make more sense? If you want to omit semicolons, generally you'll still need to place them before any line that starts with something that can be interpreted as an infix or postfix operator. So people who do this will never start a line with `[` or `(` alone, but will do `;[` or `;(`. –  May 13 '17 at 16:03
  • 1
    I know there's a campaign to not use semicolons in javascript (the so called standard.js style) but unless you really know what you are doing (strictly follow standard.js or fully understand how semicolon insertion work) I strongly suggest you end every statement with a semicolon. – slebetman May 13 '17 at 16:07
  • @AndrewLi: Unfortunately no. One would think so since the `[]` is empty, but it doesn't look ahead that far, and so it interprets the `[` as starting the member access operator, and then complains that there was no expression. –  May 13 '17 at 16:09
  • @squint I see; I would expect it to see a numeric literal and not see the following token as an access operator but the rules of ASI are strict. Since array accessing can have a line terminator a semicolon cannot be inserted. – Andrew Li May 13 '17 at 16:12
  • @slebetman: Nearly every occurrence of a problem caused by a missing semicolon that I've seen is from those who *do* include semicolons but forgot one. Those who don't include them generally know that they need one in a small handful of cases. –  May 13 '17 at 16:16
  • @AndrewLi: Yeah, would be nice if JS had less ambiguous grammar, but such is not the case. –  May 13 '17 at 16:18
  • @squint: That's completely opposite to my experience. – slebetman May 14 '17 at 01:47
  • @slebetman: Odd. Nearly every StackOverflow question I've seen that has a bug related to the kind of error above has been amongst code where semicolons are otherwise present. Those of use who elide them do so because we know the rules, so if we miss one, we can spot it immediately. People who always include them generally don't know the rules because they feel they don't need to. Just makes sense. –  May 14 '17 at 01:57
  • @squint: Again, completely opposite to my experience. All semicolon related questions I've seen are exactly like this one written by people who don't use semicolons but don't know what they're doing. – slebetman May 14 '17 at 01:58
  • @slebetman: Again, odd. In those cases where they don't use semicolons and yet haven't learned the basic rules, they won't get far before encountering such an error, at which point they learn. Your claim that *all semicolon related questions* you've seen are just like this one seems strange. I have noticed though that some opponents to this practice get very emotional over it and even to the point of irrationality at times, taking things beyond reasonable discussion. –  May 14 '17 at 02:06

0 Answers0