# javascript:void\[0\]

**URL:** <https://forum.kirupa.com/t/javascript-void-0/646038>\
**Category:** web dev\
**Created:** [February 7, 2021, 10:37pm UTC](https://forum.kirupa.com/t/javascript-void-0/646038 "2021-02-07T22:37:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [February 7, 2021, 10:37pm UTC](https://forum.kirupa.com/t/javascript-void-0/646038/1 "2021-02-07T22:37:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [February 8, 2021, 4:06am UTC](https://forum.kirupa.com/t/javascript-void-0/646038/2 "2021-02-08T04:06:28Z")

</div>

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)](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#onclick_events). Maybe there is some weird legacy encoding thing involved with that…? I’m too lazy to look into it further 😜

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 8, 2021, 6:13am UTC](https://forum.kirupa.com/t/javascript-void-0/646038/3 "2021-02-08T06:13:36Z")

</div>

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 😛

---

<div class="post-metadata">

**Author:** ![steve.mills](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/steve.mills/32/14961_2.png) [@steve.mills](https://forum.kirupa.com/u/steve.mills)\
**Post date:** [March 28, 2021, 8:44am UTC](https://forum.kirupa.com/t/javascript-void-0/646038/4 "2021-03-28T08:44:55Z")

</div>

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… 😄 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… 😄

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [April 1, 2021, 1:11am UTC](https://forum.kirupa.com/t/javascript-void-0/646038/5 "2021-04-01T01:11:35Z")

</div>

> [@steve.mills](#):
>
> I can see why `void(0)` is used. Its the shortest correct syntax for it:

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

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [April 1, 2021, 1:16am UTC](https://forum.kirupa.com/t/javascript-void-0/646038/6 "2021-04-01T01:16:04Z")

</div>

> [@senocular](#):
>
> I always thought void(…) was preferred because you could add multiple expressions in there rather than just the 0 if you wanted.

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

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

---

<div class="post-metadata">

**Author:** ![steve.mills](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/steve.mills/32/14961_2.png) [@steve.mills](https://forum.kirupa.com/u/steve.mills)\
**Post date:** [April 1, 2021, 2:03pm UTC](https://forum.kirupa.com/t/javascript-void-0/646038/7 "2021-04-01T14:03:38Z")

</div>

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? 😐

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [April 1, 2021, 6:09pm UTC](https://forum.kirupa.com/t/javascript-void-0/646038/8 "2021-04-01T18:09:53Z")

</div>

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
