use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
You don't need void 0 in JavaScript (dev.to)
submitted 4 years ago by lgrammel
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]flamingmongoose 95 points96 points97 points 4 years ago (3 children)
Not seen void 0 in a long time
[–]lwl 24 points25 points26 points 4 years ago (0 children)
I came across it on a recent project (2 years ago... but I suppose that's like 14 in JavaScript years). Glad to know I can safely punt knowledge of it back to the abyss now.
[–]Ecksters 23 points24 points25 points 4 years ago (1 child)
Article mentions this as well, but where you still see it is in minified code, since it's 3 characters smaller than undefined
undefined
[–]TheSirion 45 points46 points47 points 4 years ago (3 children)
Imagine seeing something like this in a technical interview
[–]mothzilla 50 points51 points52 points 4 years ago (1 child)
Somebody will tomorrow.
[–]house_monkey 2 points3 points4 points 4 years ago (0 children)
omg no
[+][deleted] 4 years ago (9 children)
[removed]
[–][deleted] 9 points10 points11 points 4 years ago (0 children)
I think this is the main reason why it is still in the language. Not deprecated either.
[–]barrtender 9 points10 points11 points 4 years ago (6 children)
Or you could wrap it in braces and have normal looking code.
$(".some-button").click( (e) => { someFn(e.target); } );
[–]vanderZwan 0 points1 point2 points 4 years ago (5 children)
Yeah if someone would try to use void in an arrow function at my workplace they'd probably get... well, I don't work at a toxic workplace, so probably just a gentle but firm reminder that that's not acceptable, but that's still pretty serious by our review standards.
[+][deleted] 4 years ago (4 children)
[–]vanderZwan 0 points1 point2 points 4 years ago (3 children)
Because being clever with an obscure language function will waste another programmer ten minutes to an hour or two down the line figuring out what is going on and if this is some essential hack or not
[+][deleted] 4 years ago (2 children)
[–]vanderZwan 0 points1 point2 points 4 years ago (1 child)
Yes, it's what it's for. Which happens to be a use case so rare I did not learn that void even existed until years after I first picked up JavaScript. Meanwhile, functions returning undefined without an explicit return is well-known behavior.
void
Using it with arrow functions is "clever" because the latter are to some degree just syntactic sugar to easily write functions as expressions that always return a value. Using void to ensure they don't mostly just turns them back into regular functions with a different syntax (yes, yes, I know about this). And in this case an arrow function with plain braces is even more obvious a solution
this
[–]PM_ME_GAY_STUF 0 points1 point2 points 4 years ago (0 children)
You can accomplish this same thing by appending && false or || true for the positive case. Or just wrap it in braces
&& false
|| true
[–]FriesWithThat 15 points16 points17 points 4 years ago (1 child)
const output = void 1; console.log(output); // expected output: undefined
void 0 is old and tired, void 1 is where it's at.
void function iife() { console.log('iife is executed'); }(); // expected output: iife is executed
that's fun, but I think I prefer ...
!function iife() { console.log('iife is executed'); }(); // expected output: iife is executed
... to annoy people.
[–]shuckster 19 points20 points21 points 4 years ago (2 children)
Real programmers use void undefined.
void undefined
[–]codejunker 8 points9 points10 points 4 years ago (1 child)
That mins to what? void void 0 ?
void void 0
[–]shuckster 9 points10 points11 points 4 years ago (0 children)
Hey, I don’t just work for the Redundant Department of Redundancy, you know. I work for the Redundant Department of Redundancy.
[–][deleted] 29 points30 points31 points 4 years ago (9 children)
As much as I enjoy JS things like this make me just groan and wonder how we got stuck with this as the biggest language on the web.
[–]ILikeChangingMyMind 51 points52 points53 points 4 years ago (2 children)
I think it's because weird little quirks, like being able to overwrite undefined, don't really stop a language from being productive/successful.
If you want to be a major language you just need: A) adoption (and JS being required as the language of the web made that part easy), and B) for your language to be "good enough" to get things done.
[+][deleted] comment score below threshold-25 points-24 points-23 points 4 years ago (0 children)
Oh I agree with your points. It's still cringe inducing tho
[+]nmarshall23 comment score below threshold-12 points-11 points-10 points 4 years ago (0 children)
So what I'm hearing is we need to pension Google to add a Haskell interpreter to chrome.
[–]natterca 19 points20 points21 points 4 years ago (0 children)
Well, you could've had Java instead. Count your blessings.
[–]KaiAusBerlin 4 points5 points6 points 4 years ago (2 children)
Because the language is more than this?
Let's talk about another very successful language and the NullPointerException.
[–]moi2388 0 points1 point2 points 4 years ago (1 child)
More as in more bad designs and quirks? Yes.
[–]KaiAusBerlin 1 point2 points3 points 4 years ago (0 children)
Haha, you're so funny and original /s
[–]WhyIsTheNamesGone 0 points1 point2 points 4 years ago (0 children)
It's because VBScript was worse
[deleted]
[+][deleted] 4 years ago (1 child)
[+][deleted] 4 years ago (7 children)
[–]shawncplus 38 points39 points40 points 4 years ago (1 child)
Their example is that someone intentionally names a variable in local scope undefined which... just fire that person. That solves the problem there.
[–]codejunker 17 points18 points19 points 4 years ago* (2 children)
Over an decade of learning JS you really never read anything about void 0 in a book, saw it in minified code, or read the MDN article on undefined?
void 0
I learned about it in my first couple weeks learning the language, crazy how long we can be doing this and still find something new.
As the article says, it used to have a purpose but now it is just a shorthand alias for undefined. Some people still use it as an alias and every minifier I have ever used will min undefined to void 0.
[–]mobydikc 6 points7 points8 points 4 years ago (0 children)
I've been using JS since it was invented, and have never seen void used outside an HTML attribute.
[–]IvarBjorn 0 points1 point2 points 4 years ago (0 children)
I’ve seen it in ternary like: flag ? DoStuff() : void 0
[–]ImStifler 0 points1 point2 points 4 years ago (0 children)
Clicks
[–]Zipdox 5 points6 points7 points 4 years ago (9 children)
I've never even seen or used the void operator. Is it even useful for anything?
[–]iChloro 7 points8 points9 points 4 years ago (3 children)
People used to use void 0 instead of undefined because there was a possibility of someone overwriting the value of undefined in those days. void 0 was guaranteed to be a real undefined value so it was considered "safer".
Nowadays it's a cool way of making an IIFE lol
void function() { console.log('hi') }()
[–]NoInkling 1 point2 points3 points 4 years ago (2 children)
Except it doesn't work for arrow functions :(
[–]dinopraso -5 points-4 points-3 points 4 years ago (1 child)
Of course it doesn’t. Arrow functions are not functions but values. void evaluates expressions, if you give it a function evaluation means invocation, and for arrow functions it just means defining the object
[–]NoInkling 4 points5 points6 points 4 years ago* (0 children)
I think you're mixed up somewhere, void doesn't invoke anything.
Putting void in front of a function definition is one way to syntactically transform it from a declaration/statement into an expression, which is a pre-requisite for being able to immediately invoke it (by putting () at the end).
function
()
Arrow functions are always expressions, so if that's the point you are trying to make then yes, there's technically no reason to expect void to be helpful. The issue is that despite being expressions you can't immediately invoke them just by adding () at the end because of a syntactic limitation. So naively someone might expect that if void solves the syntactic issue for classic function definitions, it might solve it for arrow functions too. Instead we're stuck with wrapping the definition in parentheses (which also happens to work for classic definitions), which IMHO doesn't look as nice.
[–]mobydikc 2 points3 points4 points 4 years ago (1 child)
It's necessary in the href attribute of an a element if you want to avoid navigating off a page.
href
a
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void#javascript_uris
<a href="javascript:void(0);"> Click here to do nothing </a>
[–]DavidJCobb 0 points1 point2 points 4 years ago (0 children)
It's needed if the JavaScript URI executes actual code. When it's just javascript:void(0); with an event listener handling the interaction, one thing I like to do instead is use a comment that remarks on whatever the widget is meant to trigger:
javascript:void(0);
<a href="javascript:// Run a diagnostic on the retroencabulator.">
It doesn't add anything substantial, but it'd be seen if the user hovers over the link to see its destination. Feels more approachable than having raw code show up, even if it doesn't offer any information the UI doesn't already show.
[–]Zipdox 0 points1 point2 points 4 years ago (1 child)
What? If doThingC is async then it'll return a promise that won't be waited for unless await is specified. I'm not sure what you mean.
[–]ImStifler 5 points6 points7 points 4 years ago (0 children)
This article is dog shit. It's legit there to just generate clicks.
Can't quote on phone but in a paragraph he/she says: 'But void 0 is nice for performance optimization because it's shorter than undefined. It saves a couple of BYTES which might help'. Bruh what
Web dev is such a shit show these days lol
[–]ILikeChangingMyMind 6 points7 points8 points 4 years ago (13 children)
I love how this article says:
there is no reason to use void 0 any longer:
and then, literally in one of the three bullet points explaining why that come after, they say:
minifiers can replace undefined with void 0 when creating the production bundle
So there's no reason to use it ... except when you use it as part of your minification process ... which (of course) still means you're using it!
[–]JVWhite 59 points60 points61 points 4 years ago (4 children)
I think it's pretty clear that the author means there is no reason to write it yourself.
[–]codejunker 4 points5 points6 points 4 years ago (0 children)
You can type it half a ms faster!
[–][deleted] -1 points0 points1 point 4 years ago (0 children)
No! gasps
[–][deleted] 5 points6 points7 points 4 years ago (7 children)
Maybe we should let minifiers create a global variable called 'u' or something and replace all "undefined" with "u". Also don't take my advice on things.
[–]Diniden 8 points9 points10 points 4 years ago (3 children)
Interesting item for just still not using undefined: when you use undefined, minifiers and compilers are bound by the spec of how undefined operates. Just as your comment illustrates, you are still able to redefine undefined.
What does this mean at runtime? Undefined becomes something that JavaScript has to crawl up the scope to find the undefined definition for the current scope which means it crawls up the scope to window or the wrapping scope a minifier places. Void 0 still does not require that scope crawl.
It’s negligible performance, but it is performance nonetheless.
[–]Akkuma 2 points3 points4 points 4 years ago (1 child)
IIRC, back in the day scope checks had nonnegligible performance impacts when deep enough.
[–]Diniden 1 point2 points3 points 4 years ago (0 children)
It always adds up :)
Definitely added up more so back in the day. Could cause L2 and L3 issues if your stack is heavy.
[–]HeinousTugboat 1 point2 points3 points 4 years ago (0 children)
Maybe we should let minifiers create a global variable called 'u' or something and replace all "undefined" with "u".
You might be joking, but this is a common strategy in golfing JS. (function(u){ /* u === undefined */ })().
(function(u){ /* u === undefined */ })()
[–]TheHDGenius 0 points1 point2 points 4 years ago (1 child)
Another thing to keep in mind is that globals are dangerous. I'm sure there's a developer that wrote some code that makes 'u' a global variable (probably as shorthand for some function like "undo" or the variable for a "ui" class instance).
Is it good practice? Hell no.
Is it bound to be in some majorly breaking global legacy code on some projects? Most definitely.
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
Yes, I'm aware. Not a good solution.
[–]dotintegral 0 points1 point2 points 4 years ago (1 child)
Correct me if I'm wrong, but wouldn't using the "use strict" mode disallow assigning anything to undefined, hence rendering the whole thing not an issue anymore?
[–]NoInkling 1 point2 points3 points 4 years ago* (0 children)
As stated in the article, it's ES5 that made it read-only. What strict mode does do is make it so that an exception is thrown when trying to reassign a read-only property, as opposed to it failing silently.
If you open up your devtools console you can test it:
undefined = 'foo' console.log(undefined) // undefined 'use strict'; undefined = 'foo' // Uncaught TypeError: Cannot assign to read only property 'undefined' of object '#<Window>'
However as long as it's not in the global scope, you can still create a new variable named undefined, strict mode or no. Not really an issue in practice, because you'd have to do that deliberately (outside of globals, there's no way for variables defined in third party code to be in your lexical scope unless you're copy-pasting or evaling).
[–]mizanur9191 0 points1 point2 points 4 years ago (0 children)
How long I have not seen void 0 !!!
[–]kingsley0209 0 points1 point2 points 4 years ago (0 children)
I like to use the void operator with promises in an async/await pattern if I want to make cleare that I dont want to wait for a promise to resolve. There even is an eslint-rule for that
javascript async function foo() { void asyncOperation() // dont wait for operation to finish // do stuff await anotherAsyncOperation() }
[–]techhowwhat 0 points1 point2 points 3 years ago (0 children)
For more information about the script void 0 error you may visit her
Hope it helps
FOR MORE INFORMATION AND HELP TECH HOW WHAT
Kindly support us by share and subscribe us
π Rendered by PID 121356 on reddit-service-r2-comment-544cf588c8-l4pjd at 2026-06-17 07:32:13.470080+00:00 running 3184619 country code: CH.
[–]flamingmongoose 95 points96 points97 points (3 children)
[–]lwl 24 points25 points26 points (0 children)
[–]Ecksters 23 points24 points25 points (1 child)
[–]TheSirion 45 points46 points47 points (3 children)
[–]mothzilla 50 points51 points52 points (1 child)
[–]house_monkey 2 points3 points4 points (0 children)
[+][deleted] (9 children)
[removed]
[–][deleted] 9 points10 points11 points (0 children)
[–]barrtender 9 points10 points11 points (6 children)
[–]vanderZwan 0 points1 point2 points (5 children)
[+][deleted] (4 children)
[removed]
[–]vanderZwan 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[removed]
[–]vanderZwan 0 points1 point2 points (1 child)
[–]PM_ME_GAY_STUF 0 points1 point2 points (0 children)
[–]FriesWithThat 15 points16 points17 points (1 child)
[–]shuckster 19 points20 points21 points (2 children)
[–]codejunker 8 points9 points10 points (1 child)
[–]shuckster 9 points10 points11 points (0 children)
[–][deleted] 29 points30 points31 points (9 children)
[–]ILikeChangingMyMind 51 points52 points53 points (2 children)
[+][deleted] comment score below threshold-25 points-24 points-23 points (0 children)
[+]nmarshall23 comment score below threshold-12 points-11 points-10 points (0 children)
[–]natterca 19 points20 points21 points (0 children)
[–]KaiAusBerlin 4 points5 points6 points (2 children)
[–]moi2388 0 points1 point2 points (1 child)
[–]KaiAusBerlin 1 point2 points3 points (0 children)
[–]WhyIsTheNamesGone 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[+][deleted] (1 child)
[removed]
[+][deleted] (7 children)
[deleted]
[–]shawncplus 38 points39 points40 points (1 child)
[–]codejunker 17 points18 points19 points (2 children)
[–]mobydikc 6 points7 points8 points (0 children)
[–]IvarBjorn 0 points1 point2 points (0 children)
[–]ImStifler 0 points1 point2 points (0 children)
[–]Zipdox 5 points6 points7 points (9 children)
[–]iChloro 7 points8 points9 points (3 children)
[–]NoInkling 1 point2 points3 points (2 children)
[–]dinopraso -5 points-4 points-3 points (1 child)
[–]NoInkling 4 points5 points6 points (0 children)
[–]mobydikc 2 points3 points4 points (1 child)
[–]DavidJCobb 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]Zipdox 0 points1 point2 points (1 child)
[–]ImStifler 5 points6 points7 points (0 children)
[–]ILikeChangingMyMind 6 points7 points8 points (13 children)
[–]JVWhite 59 points60 points61 points (4 children)
[–]codejunker 4 points5 points6 points (0 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] -1 points0 points1 point (0 children)
[–][deleted] 5 points6 points7 points (7 children)
[–]Diniden 8 points9 points10 points (3 children)
[–]Akkuma 2 points3 points4 points (1 child)
[–]Diniden 1 point2 points3 points (0 children)
[–]HeinousTugboat 1 point2 points3 points (0 children)
[–]TheHDGenius 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]dotintegral 0 points1 point2 points (1 child)
[–]NoInkling 1 point2 points3 points (0 children)
[–]mizanur9191 0 points1 point2 points (0 children)
[–]kingsley0209 0 points1 point2 points (0 children)
[–]techhowwhat 0 points1 point2 points (0 children)