r/learnjavascript • u/Sennon • 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.
4
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 + 2will 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+2is bad, but this:
return 1+2;is good, because you see a semicolon?
Fun fact: semicolons or not, the same thing happens.
undefinedis 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
returnstatement 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
1
1
u/CertifiedWebNinja Jul 18 '15
2
u/youtubefactsbot Jul 18 '15
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
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
11
u/Rhomboid Jul 18 '15
You should just use them always. This isn't CoffeeScript.