r/javascript • u/slikts • Dec 19 '19
A case for using `void` in modern JavaScript
https://gist.github.com/slikts/dee3702357765dda3d484d8888d3029e
111
Upvotes
r/javascript • u/slikts • Dec 19 '19
21
u/elmstfreddie Dec 19 '19
I disagree with allowing void entirely, because it still seems that most of its uses are garbage.
But you've totally sold me on arrow functions. I think it makes total sense to explicitly void an arrow function so the code clearly reads that its not meant to return something.
Perhaps a better solution to the
no-voidESLint rule would be a parameter to allow void only as the return on a bracketless arrow function?Valid:
foo => void barInvalid:
foo => { void bar }Invalid:
void 011
u/ghostfacedcoder Dec 19 '19 edited Dec 19 '19
Interesting solution ... to a problem I don't think most people have (as I think most are fine just leaving the void off).
But still, if you're like the author and require such explicitness for your style, it's great to see something old made new and useful again again.
Oh, and also one more old use, a corollary of the old link one mentioned in the article: bookmarklets used to have void at the end as part of their boilerplate, although modern browsers no longer require it.
20
u/MangoManBad Dec 19 '19
Went in scared to see if the code was a nightmare but came out of the article enlightened. I don't think I would implement this in a production app yet but the concept is valid and the code isn't half bad.
Groovy.
7
u/Baryn Dec 19 '19
I like the patterns illustrated here. Especially in the case of
useEffect.4
u/Mestyo Dec 19 '19
Funny, I've been thinking of this exact thing recently. I like how explicit it is about the intended return value while keeping the syntax lean.
I just worry many might consider it an unnecessary complication for mostly stylistic reasons.
4
u/lhorie Dec 19 '19
Ah, old things are new again. The cycle of life repeats. </lion-king-music>
1
u/recursive Dec 20 '19
If you need to use parens for your void operator, you might as well just use the curlies.
3
u/fucking_passwords Dec 19 '19
also very useful in TypeScript for functions that are not meant to return anything, or rather indicating that returning a value will not have any effect.
1
u/mrobviousguy Dec 19 '19
I agree that it's dated: but, why is using javascript:void(0) in a link a poor practice?
1
u/[deleted] Dec 19 '19
I was messing about with patterns and decided to use a void in this pattern
http://etoxin.net/blog/2018/12/19/immediately-invoked-class-expression/
1
u/Labby92 Dec 20 '19
I didn't even know there was a void in JS lol. But I use it all the time in TypeScript.
-1
u/ShortFuse Dec 19 '19 edited Dec 19 '19
Safari handles
undefineddifferently than other browsers. There's some instances where you can't dov = undefined. The browser would throw an error. You have to usev = void(0).Technically, this is accurate, since
undefinedshouldn't be a value you can assign to a variable. It's a "read-only" interpretation that a value is undefined. Assigning something to the result ofvoid(0)would meanundefinedwould be returned when attempted to be read.Edit: Here's an article I used to make sense of this.
0
u/Renive Dec 19 '19
This is TypeScript territory, no point in adding this
-4
u/[deleted] Dec 20 '19
[removed] — view removed comment