Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semicolons in JavaScript #18

Closed
joshhartigan opened this issue Oct 17, 2014 · 10 comments
Closed

Semicolons in JavaScript #18

joshhartigan opened this issue Oct 17, 2014 · 10 comments

Comments

@joshhartigan
Copy link

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)

@max-mapper
Copy link
Owner

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')
}

@joshhartigan
Copy link
Author

thanks for your answer!

@fiveisprime
Copy link

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 (){ }()

👍

@ghost
Copy link

ghost commented Mar 20, 2015

What about:

var x = y
!function(){}()

@ivantodorovich
Copy link

var x = y
void function (){ }()

This is actually straightforward!
Love it! 😍

@ricardohbin
Copy link

other ones :P

~function (){ }()
+function (){ }()
-function (){ }()

@kevincennis
Copy link

you guys this is clearly the best and most readable way

var x = y
typeof function (){ }()

@mattbasta
Copy link

This one is obviously the most performant:

var x = y
delete function () {} ()

@ghost
Copy link

ghost commented Apr 2, 2015

And now I win:

var x = y
new function(){}

Look ma, no call!

@max-mapper
Copy link
Owner

as much as I enjoy bad humor I am gonna hit my gmail space limit if this keeps goin

Repository owner locked and limited conversation to collaborators Apr 2, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants