4 ms·
>Just end lines with `;`. But that's not always the solution, you still need to know when ASI kicks in and when it doesn't. "just end lines with ;" isn't true
by Klathmon 8y ago
>Just end lines with `;`.
But that's not always the solution, you still need to know when ASI kicks in and when it doesn't.
"just end lines with ;" isn't true in multi-line arrays, in multi-line objects, and a ton of other situations where a semi at the end of the line is a syntax error. And while i'm sure you know that and didn't actually mean "every" line, it just goes to show that you already have the knowledge of when to add them and when not to. But that knowledge is learned, and it isn't a simple rule by any means.
For example, the following code is probably not going to do what many beginners think it will do:
return
[
1,
2,
3,
];
Because the JS engine will insert a semicolon right after the return, and you will basically get `return undefined;` as the result.
Semicolons at the end of lines or not, you still need to understand when they are inserted and when they aren't. The real solution is to use a linter which will check these cases for you. I don't care if you use them or don't, just make sure something else enforces it.
- joshuamorton 8y agoYou're absolutely correct. Jey provided the better phrasing of my comment: Use `;` to end a statement. The point I'm making is more bias toward using `;`, even when not strictly "necessary". This brings JS's statement parsing in line with most other languages. And yes, absolutely use a linter.
- Klathmon 8y agoOr you can do what a past coworker did. We settled on standard.js for our linting in a project, and they don't use semicolons. The coworker hated this, so he setup his editor to auto-format into his preferred style on load, then on save use the auto-format option in standard.js to save it as the format that we agreed on in the repo. He only ever worked with his favorite layout, and we worked with ours! I'm not a big fan of holy wars in programming, i'm fine with either even if I do prefer one style over the other, but at the end of the day I just hope that everyone involved understands that it's just a preference thing for the most part. Linters will catch the edge cases for both styles, and if you really can't adapt to using or not using semicolons in javascript, there are probably other issues at play.
- ezekg 8y agoThis made me laugh out loud. I identify with the sentiment (I can get pretty pedantic about how code looks), but I wouldn't take it to that extreme.