0

My code is this:

message.labels.forEach(/…/) container.data.push(message);

It throws Uncaught SyntaxError: Unexpected identifier When I add a semicolon after the forEach function it doesn't throw it anymore. I only change the semicolon there, I checked it with my git.

Why does it do it? Feels like I'm missing some JS fundamentals here.

Mihkel L.
  • 1,367
  • 20
  • 35
  • What do you think the code does? Looks like a lack of understanding loops. – Bergi Feb 05 '16 at 14:22
  • 1
    Use linting tool like JsLint, JsHint, JSCS, EsLint. That will prevent strange bugs like your one: putting two comments on one line. – Niels Steenbeek Feb 05 '16 at 14:22
  • @Bergi The loop you're seeing does nothing. the `/.../` part is there to show, there is some content. – Mihkel L. Feb 05 '16 at 14:26
  • @NielsSteenbeek yeah, thanks for hint. New-line also removes the error. – Mihkel L. Feb 05 '16 at 14:27
  • @MihkelL.: OK, well, it might have been possible that you'd have expected the `container.data.push(message);` to be the loop body. You really should place statements on separate lines - and even then use semicolons although ASI would do it. – Bergi Feb 05 '16 at 14:30
  • I used ctrl+x and it cut the statement to same line, and then error came that I didn't expect. usually those things are of course on different lines. Probably that's why I didn't know it before. ;) – Mihkel L. Feb 05 '16 at 14:36

1 Answers1

2

The semi-colon ends a statement.

If you leave it out, then your two statements are treated as a single one, it doesn't make any sense and the compiler throw an error.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • Yeah, but usually browser can handle it. Line change also removes the bug. Guess browser need it then. :) – Mihkel L. Feb 05 '16 at 14:23
  • 2
    @MihkelL.: [ASI only works on linebreaks](http://stackoverflow.com/q/2846283/1048572) – Bergi Feb 05 '16 at 14:31
  • @MihkelL. - JavaScript has well-defined rules about what to do with lines that don't end in a semicolon. A line-break doesn't necessarily end a statement, though often it does. – nnnnnn Feb 05 '16 at 14:33