Semicolons in JavaScript
joshhartigan opened this issue · 10 comments
Just wondering, why don't you use Semicolons in JavaScript? Isn't it safer - and only costs a few extra bytes? (Not trying to start a flamewar, just interested in your take on it)
hey, thanks for asking! I just prefer it, and think it boils down to personal style. here are some random thoughts:
I dont keep them out for byte size reasons, byte size of source code syntax/style isn't really important since minifies will take everything out anyway. I do it because I havent found them to be necessary myself
I'm not sure what cases people get caught by not using semicolons, but it doesnt happen to me so I am happy not using them
The two cases I know to avoid are:
// some var with an IIFE on the next line
var x = y
(function(){})()
which gets interpreted as var x = y(function(){})()
but I avoid it by not using anonymous inline functions and writing it like this instead:
var x = y
function z(){}
z()
which accomplishes the same thing but doesnt use the IIFE pattern which I think is ugly anyway
the second case is:
return
a
which gets interpreted as return; a;
. But it's easy: dont put a newline after your return
statements. I actually use this early return pattern all the time in my code, e.g.:
function doStuff() {
if (notReady)
return
console.log('will only get here if ready')
}
thanks for your answer!
Sorry to jump into a closed thread, but I've been working through thoughts on life without semicolons and came up with an additional solution to an IIFE after var
declaration
var x = y
void function (){ }()
👍
What about:
var x = y
!function(){}()
var x = y
void function (){ }()
This is actually straightforward!
Love it! 😍
other ones :P
~function (){ }()
+function (){ }()
-function (){ }()
you guys this is clearly the best and most readable way
var x = y
typeof function (){ }()
This one is obviously the most performant:
var x = y
delete function () {} ()
And now I win:
var x = y
new function(){}
Look ma, no call!
as much as I enjoy bad humor I am gonna hit my gmail space limit if this keeps goin