I was hovering over a link recently and saw one with an href of javascript:void[0]. This was pretty surprising, since it’s usually written as javascript:void(0). Granted, the whole thing is supposed to imply meaningless anyway, but I wonder what led someone to use this variant.

Makes me wonder if void 0 would cause some sort of issue with spaces and URI encodings, or if void(0) was popularized because it looks like a function call even though it isn’t.

1 Like

I’m feeling confident that they wanted void(0) but just typed it wrong or didn’t know any better.

I always thought void(…) was preferred because you could add multiple expressions in there rather than just the 0 if you wanted. Though I have done this before, I’d also guess no one else probably does it and probably sticks to void 0 because its the thing they’ve read to do, and probably using the format void(0). So who was the first person to suggest this and how did they present it? MDN currently references it as void(0). Maybe there is some weird legacy encoding thing involved with that…? I’m too lazy to look into it further :stuck_out_tongue_winking_eye:

1 Like

I never thought about using anything but void(0). Whoever came up with that decades earlier probably didn’t have a good reason either and is probably having a good chuckle at all of us trying to find the hidden meaning behind it :stuck_out_tongue:

1 Like

I got curious and played with it a bit.
I can see why void(0) is used. Its the shortest correct syntax for it:
void 1+3; // Nan
void // throws expression expected
void() // throws expression expected
void a // throws a is undefined
void(a) // throws a is undefined

void(1+3) // Undefined as expected
void(null) undefined
void(undefined) undefined
void 'a' // undefined
void('a') // undefined
void 0 // undefined
void(0) undefined
void [0] undefined
void [0].concat([1]) undefined
void {} undefined
void [] undefined

Void needs a primitive or object to evaluate before returning undefined.
I think I might start using void 0 just to confuse people.

I recon they were just trying to save on keystrokes. Brendan’s probably having a laugh… :smile: apparently he sponsored Rust at Mozilla/ Netscape. That dude knows how to pick winners…
Maybe we could crowd fund a race horse and call it Brendan Eich… :smile:

Can you explain why void(0) is shorter or more correct than void[0] or void 0, as I mentioned in the initial post?

Sure, but you can do that with void[...]:

void[console.log('hey'), 'senocular']

Edit: And yeah, I realize that the more intuitive syntax to reach for is () over []. The whole premise for this thread is that I saw the opposite in a random IRL web encounter.

I don’t completely get it.
It seems that void kind of acts like an IIFE with no return value.
It doesn’t stop anything running inside just stops the return.
And will make any reference to a named function inside undefined.

-Void will stop any function being called to its right (inside or outside of parentheses) unless it is an IIFE.
void function logger(){blah} // undefined
void (function logger(){blah})()// runs

  • Void will throw any variable var let const but not named or anonymous function
    void let logger = () => console.log('blah') //throws unexpected

  • Void will evaluate any expression then return undefined if it is wrapped in parentheses
    but will not return undefined if not in parentheses (unless its a function except IIFE)
    void 2 == '2'; // returns false
    void (2 == '2'); // returns undefined
    void 1+3; // Nan
    void(1+3) // undefined

So I think the parentheses guarantees the return value undefined.

I’ve been trying to think of any other use case for void but I just cant really. An IIFE can do the same without throwing variables.

Maybe they haven’t deprecated it yet cause some libraries use it for some black magic…
Any ideas? :neutral_face:

void always returns undefined. When it comes to the parentheses, they are what allows you to control the order of operations because without them

void 2 == '2';

is basically seen as

(void 2) == '2'; 
// or
undefined == '2';

That’s basically all void does, is given an expression, returns undefined rather than the expression’s resulting value

1 Like