Hacker News new | past | comments | ask | show | jobs | submit login

That is a sensible way to think about it, and it would be great if the language worked that way, but unfortunately it does not. Statements in JavaScript are not separated by line breaks.

Here is an example to illustrate:

    console.log('a')
    (1 < 2) ? console.log('b') : console.log('c')
You might expect this to output 'a', then 'b'. However, it instead outputs 'a' and then throws an error like this:

    Uncaught TypeError: console.log(...) is not a function
...because a semicolon was not inserted at the end of the first line.



What about "statements are separated for you" implies line breaks split valid statements? The issue in your example is your first statement "console.log('a')(1 < 2)" tries to pass "true" to the return value of console.log triggering a runtime typeError. If you define an appropriate function for console.log (remember it's not part of JS) e.g.

    console.log = function(value){console.error(value);return function(value2){console.error(value2)}}
Your example runs just fine because there was never actually a syntaxError anywhere in it to begin with, let alone a sytanxError that could be fixed with a ; by ASI. Similarly if I define console.log = 2 all of the above will throw typeError but that also has nothing to do with ASI.

This is precisely what "but if you need them to be separated in a special way you can add semicolons to manually control separation behavior" was referring to.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: