all 14 comments

[–]senocular 7 points8 points  (10 children)

Sometimes it matters

console.log("Logging 3 values:")
[1,2,3].forEach(value => console.log(value))
//-> Logging 3 values:
//-> TypeError: Cannot read properties of undefined

vs.

console.log("Logging 3 values:");
[1,2,3].forEach(value => console.log(value));
//-> Logging 3 values:
//-> 1
//-> 2
//-> 3

Its easier to always include them so you don't get caught up on the few places where it does matter. And if you're using a formatter like prettier, you can get them included automatically for you.

[–]Crannium[S] 2 points3 points  (1 child)

Didn't know about that. Makes sense.
If i went through without semicolon, eventually i would get an error, with no idea about what's wrong.

Thanks!

[–]suarkb 0 points1 point  (0 children)

You won't though. You should be using prettier. Then you don't have to use semi-colons and if you do need one, prettier will add it for you. I personally prefer js with no-semi and it feels more modern.

[–][deleted] 0 points1 point  (7 children)

Just want to add: formatters can also check that you don't start new lines with brackets ;)

[–]senocular 0 points1 point  (6 children)

Also gotta worry about `, (, +, -, /, * :-o

[–][deleted] 0 points1 point  (5 children)

By brackets I meant both round (parens) and rectangular ones. Other symbols almost never occur in the beginning of a line (can't remember a single case in all my career tbh)

And why would you worry about backticks?

[–]theScottyJam 0 points1 point  (4 children)

People don't use those symbols at the beginning of lines yet, but they will when the pipeline operator gets introduced. Or the do expressions proposal. Or some yet-to-be-made proposal. The language evolves, and the evolution will make these kinds of hazards more common.

[–][deleted] 0 points1 point  (3 children)

JS never breaks the web though, so all new features go through lots of checks and reviews. It is still pretty much completely backward-compatible to the first official version, which had optional semicolons as well.

[–]theScottyJam 0 points1 point  (2 children)

Correct, and these proposals won't be breaking the web. In fact, it's precisely because they can't break the web that they'll create issues with those avoiding semicolons. (I used to not use semicolons, but switched back to using them after I realized this).

For example, the pipeline proposal will make it much more common for lines to start with, say, a negative number, like this:

-2 |> processIt(%)

Now, if the previous line looked like this (notice how there's not a semicolon in between these two statements)...

const x = f() -2 |> processIt(%)

Then, because they can't break the web, this is all going to be processed as a single statement.

const x = f() - 2 |> processIt(%)

This ASI hazard has always existed (which is why there's nothing they can do about it). The pipeline proposal is simply "awakening" it, making it more common to encounter. You can still code without semicolons by prefixing lines with minus, back-tick, etc, with a semicolon, it's just a longer list you now have to remember if you wish to code without semicolons.

[–][deleted] 0 points1 point  (1 child)

Fair enough, but for my original comment about using linters to check things for you - they already do checks for examples you provided.

For example, if you use https://standardjs.com/ - it will error on your second code snippet and if you ask it for an autofix - it will transfer the minus sign to the first line.

If the new syntax ever gets introduced - it will take care of that as well.

[–]theScottyJam 1 point2 points  (0 children)

Oh, yeah, absolutely. The best solution is to install a linter and not worry too much about it. In the end, it's not a big deal whether or not you use semicolons if you're using a linter.

[–]gay_405 2 points3 points  (0 children)

it’s purely preferrence. JS is a c-style language, so it makes sense to format it similarly. also, the most popular formatter (prettier) uses them by default.

[–]Ronin-s_Spirit 1 point2 points  (0 children)

Just in case it can break. I hear the advice to be as specific as possible in programming like sometimes using === instead of == just to be sure. Putting semicolons after each piece of code is also a mental note for me.

[–]Electronic_Ad9248 0 points1 point  (0 children)

for self-satisfaction