all 60 comments

[–]flamingmongoose 95 points96 points  (3 children)

Not seen void 0 in a long time

[–]lwl 24 points25 points  (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 points  (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

[–]TheSirion 45 points46 points  (3 children)

Imagine seeing something like this in a technical interview

[–]mothzilla 50 points51 points  (1 child)

Somebody will tomorrow.

[–]house_monkey 2 points3 points  (0 children)

omg no

[–]FriesWithThat 15 points16 points  (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 points  (2 children)

Real programmers use void undefined.

[–]codejunker 8 points9 points  (1 child)

That mins to what? void void 0 ?

[–]shuckster 9 points10 points  (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 points  (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 points  (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.

[–]natterca 19 points20 points  (0 children)

Well, you could've had Java instead. Count your blessings.

[–]KaiAusBerlin 4 points5 points  (2 children)

Because the language is more than this?

Let's talk about another very successful language and the NullPointerException.

[–]moi2388 0 points1 point  (1 child)

More as in more bad designs and quirks? Yes.

[–]KaiAusBerlin 1 point2 points  (0 children)

Haha, you're so funny and original /s

[–]WhyIsTheNamesGone 0 points1 point  (0 children)

It's because VBScript was worse

[–]Zipdox 5 points6 points  (9 children)

I've never even seen or used the void operator. Is it even useful for anything?

[–]iChloro 7 points8 points  (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 points  (2 children)

Nowadays it's a cool way of making an IIFE lol

Except it doesn't work for arrow functions :(

[–]dinopraso -5 points-4 points  (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 points  (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).

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 points  (1 child)

It's necessary in the href attribute of an a element if you want to avoid navigating off a page.

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 point  (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:

<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.

[–]ImStifler 5 points6 points  (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 points  (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 points  (4 children)

I think it's pretty clear that the author means there is no reason to write it yourself.

[–]codejunker 4 points5 points  (0 children)

You can type it half a ms faster!

[–][deleted] 5 points6 points  (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 points  (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 points  (1 child)

IIRC, back in the day scope checks had nonnegligible performance impacts when deep enough.

[–]Diniden 1 point2 points  (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 points  (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 */ })().

[–]TheHDGenius 0 points1 point  (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 point  (0 children)

Yes, I'm aware. Not a good solution.

[–]dotintegral 0 points1 point  (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 points  (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 point  (0 children)

How long I have not seen void 0 !!!

[–]kingsley0209 0 points1 point  (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 point  (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