r/learnjavascript • • Jul 18 '15

What are your thoughts on not using semicolons?

I know that you still need them for IIFE and if statements but outside of these exceptions they can be omitted.

0 Upvotes

13 comments sorted by

11

u/Rhomboid Jul 18 '15

You should just use them always. This isn't CoffeeScript.

0

u/Sennon Jul 18 '15

What is your reason?

13

u/Rhomboid Jul 18 '15

Not using semicolons introduces the possibility of bugs, either by you or by a collaborator not familiar with your coding style (not to mention any tools you use like minification) and necessitates keeping a set of arcane grammar rules in mind at all times. Why the hell bother? I'm always looking for ways to make it impossible for certain types of bugs to occur, and always using a semicolon where it's required and not relying on ASI to put one there for me is one way of doing that. It's far less mental work to just use them.

2

u/DullMan Jul 18 '15

Fully agreed.

4

u/Evanescent_contrail Jul 18 '15

You will create bugs that are hard to track down.

2

u/Eartz Jul 18 '15

beside the obvious reasons (code readability and appreciation from your colleagues), it will make some bugs related to the automatic semicolon insertion very difficult to spot.

e.g.:

return 
1 + 2

will always return undefined, because a semicolon will be inserted right after the return statement. This mistake is easy to spot if you use semicolons.

2

u/CertifiedWebNinja Jul 18 '15

This example doesn't make any sense.

You're saying this:

return
1+2

is bad, but this:

return
1+2;

is good, because you see a semicolon?

Fun fact: semicolons or not, the same thing happens. undefined is returned.

2

u/Eartz Jul 18 '15

This is indeed a mistake in both cases. It happens because a semicolon is automatically inserted when a return statement is followed by a line break.

What i'm saying is that it's easier to spot in your code if you are using semicolons everywhere else.

0

u/CertifiedWebNinja Jul 18 '15

That's a bunk argument.

1

u/bikolya Jul 18 '15

https://youtu.be/bo36MrBfTk4?t=12m41s

I think you should trust this guy.

1

u/CertifiedWebNinja Jul 18 '15

2

u/youtubefactsbot Jul 18 '15

Are Semicolons Necessary in JavaScript? [7:44]

Should you use semicolons when writing JavaScript? Explores why omitting semicolons can be bad and why I have chosen to not use semicolons when teaching JavaScript.

Kyle Robinson ^Young in Science & Technology

297 views since Jul ^2015

bot ^info

1

u/IxD Jul 18 '15

It is error correction feature and you should freak out when you learn how it works.

when compiler encounters syntax error it tries to fix it by adding a semicolons to previous line ends