5 ms·
You can skip semicolons in Javascript and Typescript in all but the rare cases where there is ambiguity. That's what I do and there is zero problem.
by eevilspock 6y ago
You can skip semicolons in Javascript and Typescript in all but the rare cases where there is ambiguity. That's what I do and there is zero problem.
- adamredwoods 6y agoAm I correct in stating that standard Typescript no longer requires semi-colons?
- Sebb767 6y agoYes. This has been the case for at least two years now.
- _630w 6y agoBut don't please. Semicolons are not optional. When you don't put your semicolons, they will inserted by the parser and it can introduce some buggy code especially if you are using some formatter.
- feross 6y agoThe behavior of ASI is fully specified. Semicolons are optional. There is no undefined behavior in the language spec that makes using semicolons somehow safer. Furthermore, even if you always write semicolons you are still subject to ASI modifying your code by inserting additional semicolons where you don’t want them. So you need to learn the rules of ASI regardless of whether you use semicolons or not. https://feross.org/never-use-semicolons/ https://feross.org/never-use-semicolons/
- searchableguy 6y agoThanks for the link!
- CuriousSkeptic 6y agoThere is no ASI in “strict mode”. And modules are always in strict mode. Edit: I was mistaken about strict mode.
- pygy_ 6y agoThat is just not true. You can write semi-less JS modules, strict mode and ASI are orthogonal.
- CuriousSkeptic 6y agoIndeed, just read the spec: https://tc39.es/ecma262/#sec-automatic-semicolon-insertion https://tc39.es/ecma262/#sec-automatic-semicolon-insertion No mention of strict mode in the section of ASI. Could have sworn that the ASI issues I’ve ran into in the past was resolved by using strict mode, not sure what I’ve got that from.
- realharo 6y agoCan you show an example of such a bug? The only thing I can think of is return value which will get flagged by TS as unreachable code (you can try it in https://www.typescriptlang.org/play https://www.typescriptlang.org/play)
- searchableguy 6y agoI can't think of a good real world example atm but Say, you have let a = 1 let b = 2 (a + b) <--- b won't be accessible here. This will also get flagged by ts. But not everyone uses ts. /shrugs Feross is right, I guess.
- realharo 6y agoI think eslint would flag it as well for what it's worth (https://eslint.org/demo https://eslint.org/demo, you have to set it to newer ES version in the Rules configuration at the bottom of the page).
- deleted 6y ago[deleted]