2

When writing Javascript without semicolons, \n becomes the idiomatic statement terminator, but sometimes it is actually not, which I would like to express in my code. So is there a special character that explicitly prevents Automatic Semicolon Insertion, to alleviate this uncertainty?

Please note that I am actually in favor of using semicolons where possible, but some of the people I work with want to omit them.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Isabaellchen
  • 98
  • 10
  • 4
    Please post an example. – axiac Jul 23 '18 at 08:11
  • 2
    When [your question is closed](https://stackoverflow.com/questions/51472364/javascript-is-there-a-character-for-linebreaks-in-a-statement) you should improve it and allow it to be reopened. Deleting it and asking it again can be unpopular. – Quentin Jul 23 '18 at 08:17
  • 1
    Thanks for the advice @Quentin. Seeing the reactions to this topic, i did not believe the question would have been reopened. – Isabaellchen Jul 23 '18 at 08:37
  • @Isabaellchen: _why_? What would be the use? – Cerbrus Jul 23 '18 at 09:43
  • @Cerbrus Semicolons are being omitted because some say writing without them is faster, i can sort of understand the notion, even though i do not like it. It creates gaping holes and people having to read the code later might get confused, since i create multiline statements to stay under 74 chars per line. So to at least counter the confusion a bit, id like to have a way of explicitly writing a multiline statement – Isabaellchen Jul 23 '18 at 11:32
  • 1
    Yes, semicolons are "optional". But there isn't a single serious programming style out there that doesn't make them mandatory. Tell your coworkers to get used to it. It'll save them a lot of trouble in future (JS) jobs. I mean, sure, _technically_, it saves a keystroke. Whoop-tee-doo, you can use that saved time debugging your application when you're minimizing it! – Cerbrus Jul 23 '18 at 11:34
  • 2
    _Yes, semicolons are "optional"_ gave me an allergic reaction. I know that you can write javascript statements without using that `;` but even then it is **wrong**. It is NOT fool proof. Incorrect use of it _can lead to bugs_. I sneeze if I see a large javascript code file without semicolon. Moreover, it leads to **inconsistency** because there are keywords that must have semicolons (see the specs dammit). So, OP I am glad that you keep using the `;` and I hope that you never would give up and start programming without using `;` ..... – KarelG Jul 23 '18 at 11:54
  • @Cerbrus Showing them this whole thread will definetly help the discussion. – Isabaellchen Jul 23 '18 at 11:55
  • 1
    "but some of the people i work with want to omit them" -- volunteer to setup an ESLint config for your project. And in that config, you include a rule to require semicolons... Or just use an existing config such as `eslint-config-airbnb`. Then nobody can blame you for just using the style *you* prefer. – ThiefMaster Jul 23 '18 at 12:19
  • Turns out the JS standard says: omit them https://github.com/standard/standard ¯\\_(ツ)_/¯ – Isabaellchen Aug 06 '18 at 18:52

2 Answers2

3

Yes, ; is what you are looking for.

Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • As far as I understand it, OP is asking for a line continuation character, which is the opposite of what you're pointing to. – Robby Cornelissen Jul 23 '18 at 09:11
  • 3
    @RobbyCornelissen the question is "Is there a special character to prevent ASI" ASI stands for "**Automatic** Semicolon Insertion". `;` will do the job. – Kaiido Jul 23 '18 at 09:12
  • Stating that manual insertion prevents automatic insertion, albeit semantically somewhat clever, does nothing to bring the OP closer to the goal stated in the question. As such, `;` is *not* what the OP is looking for. – Robby Cornelissen Jul 23 '18 at 09:29
  • 1
    @RobbyCornelissen (that's the joke) – Kaiido Jul 23 '18 at 09:35
  • 1
    as much as you might be funny, this question doesn't answer the question and is against the spirit of SO. Maybe make it very clear that it's a joke, so people who are actually trying to find an answer (and remember that just like OP, they might also dislike semicolon-free codebases regardless) do find it. – towc Jul 23 '18 at 12:16
  • 1
    @towc: __Q:__ _"Is there a special character to prevent ASI"_ - __A:__ _"Yes, `;` is what you are looking for."_. That's a perfectly valid and correct answer. – Cerbrus Jul 23 '18 at 12:17
  • 1
    @towc IMM best jokes are the serious ones. If one needs this Q/A, what they really need is a semicolon. – Kaiido Jul 23 '18 at 12:47
  • @Kaiido then how about we go to all the python Qs, and tell them that they should just use javascript instead? – towc Jul 23 '18 at 13:43
  • @towc python is about the worst example you could have chosen isn't it? If they don't need semicolon, it's because if you don't follow their strict code style the program will simply not execute. – Kaiido Jul 23 '18 at 14:19
0

Yes, it exists by using the grouping operator (). No ASI happens inside of the parentheses.

return (
    // expression
);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Note that using this to prevent "ASI" is a massive counter-pattern. It's a hack. – Cerbrus Jul 23 '18 at 11:36
  • it's just a language featue, no hack. – Nina Scholz Jul 23 '18 at 11:41
  • The reverse psychology on this topic is strong, i sincerely apologize for that.... – Isabaellchen Jul 23 '18 at 11:41
  • 1
    @NinaScholz: An abused language feature is still a hack. – Cerbrus Jul 23 '18 at 11:41
  • 4
    @Cerbrus, even if you don't like the question, this answer answers it. you may add a remark inside of the answer to express your discomfort, but it solves the problem. i have used parentheses in this pattern as well, you might find it, as you know all of my answers. – Nina Scholz Jul 23 '18 at 11:51
  • What are you trying to achive? I have yet to see a valid use of _"parenthesis to prevent ASI"_. I still think it's a hack. – Cerbrus Jul 23 '18 at 11:53