<p>In JavaScript, there is an operator <code>void</code> that, given an expression, evaluates it but then ignores the result and evaluates to <code>undefined</code>:</p>
<pre><code>let a = void 1+1;
console.log(a);
// undefined
</code></pre>
<p>The use case for <code>void</code>, at least nowadays, is extremely niche. <code>void 1</code> is a slightly shorter way of writing <code>undefined</code> which helps save a few bytes of bandwidth. It's also used to explicitly discard the result of functions you are calling for their side effects.</p>
<p>Both of these can be very easily emulated using other language features though. I know of no other language that has an operator like this. Why did JavaScript include <code>void</code>?</p>
<p>Ideally, answers would cite real references and refrain from baseless speculation. I'm specifically looking for the reason it was added in the first place, not modern-day uses.</p>

<history><javascript>
Why did JavaScript choose to include a void operator?
CC BY-SA 4.0
2023-08-25T19:08:53.993Z
[This](https://stackoverflow.com/questions/666936/what-is-the-point-of-void-operator-in-javascript) thread might provide some answers.
Luke LaBonte
CC BY-SA 4.0
2023-08-26T19:41:40.387Z
_"I know of no other language that has an operator like this"_ — in C you can write `(void)foo(bar);`, which would ignore the return value of `foo` and (usually) suppress a diagnostic about unused value.
Ruslan
CC BY-SA 4.0
2023-08-26T19:42:15.933Z
@Ruslan That's really just a special case of casting though
mousetail 'he-him'
CC BY-SA 4.0
2023-08-26T19:43:09.777Z
Yes, but this special case is explicitly spelled out in the Standard, see [this answer](https://stackoverflow.com/a/34289000/673852). (oh, it's about C++ actually)
Ruslan
CC BY-SA 4.0
2023-08-28T21:24:51.637Z
"*A special case of casting*": yes, and what? `(void)` is an operator in C, both semantically, and in the terminology of the language spec.  It has an effect substantially analogous to JS's `void` operator.  How is it relevant that C also has other cast operators?
John Bollinger
<p>The most compelling reason for having it in the language, seems to be for its use in <code>javascript:</code> URLs, such as in the <code>href</code> attribute of an <code>&lt;a&gt;</code> tag (example from <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void" rel="noreferrer">this MDN article</a>):</p>
<pre class="lang-html prettyprint-override"><code>&lt;a href=&quot;javascript:void(document.body.style.backgroundColor='green');&quot;&gt;
  Click here for green background
&lt;/a&gt;
</code></pre>
<p>The code in a <code>javascript:</code> URL is evaluated as an expression, and the result ─ if it is not <code>undefined</code> ─ is used (as a string), as the link URL in this case. But here, the assignment expression produces the string <code>'green'</code>, which is not supposed to be a link address; so the <code>void</code> operator is needed to discard that string, so the browser doesn't navigate away from the page when the link is clicked.</p>
<p>The same applies to <code>javascript:</code> links loaded directly in the address bar, from the browser's bookmarks or history, or otherwise. Discarding the values produced by these links is confirmed as the actual reason for having the <code>void</code> operator, in an email from Javascript creator Brendan Eich (<a href="http://web.archive.org/web/20140925082204/http://www.sitepoint.com/bookmarklets/" rel="noreferrer">web.archive.org link</a>):</p>
<blockquote>
<p>In particular, I made it possible to generate a new document by loading, e.g. <code>javascript:'hello, world'</code>, but also (key for bookmarklets) to run arbitrary script against the DOM of the current document, e.g. <code>javascript:alert(document.links[0].href)</code>. The difference is that the latter kind of URL use an expression that evaluates to the <code>undefined</code> type in JS. I added the <code>void</code> operator to JS before Netscape 2 shipped to make it easy to discard any non-<code>undefined</code> value in a <code>javascript:</code> URL.</p>
</blockquote>
<hr />
<p>MDN suggests a couple other possible uses for the <code>void</code> operator, but they aren't so compelling:</p>
<ul>
<li>It allows an <a href="https://en.wikipedia.org/wiki/Immediately_invoked_function_expression" rel="noreferrer">IIFE</a> to be used where a statement would be expected; but parentheses also work for this.</li>
<li>It allows discarding the result of an arrow function when a callback should be provided which doesn't leak a return value; but braces around the body of the arrow function also work for this.</li>
</ul>
<p>Other proposed uses include it being a reliable way to get an <code>undefined</code> value before the global constant <code>undefined</code> was added to the language, and it being a way to silence linter warnings about unused results of some operations.</p>

CC BY-SA 4.0
2023-08-25T19:25:35.810Z
2023-08-28T18:51:13.837Z
2023-08-28T18:51:13.837Z
kaya3
CC BY-SA 4.0
2023-08-25T19:34:37.687Z
It can also be used to make a linter shut up about fire-and-forget promises, but most of the time you at least want `.catch(() => {})` to prevent unhandled rejection errors.
Bbrk24
CC BY-SA 4.0
2023-08-25T19:42:53.710Z
`javascript:undefined` works for a href too though. And most of your other reasons relate to newer uses that didn't exit when `void` was first added
mousetail 'he-him'
CC BY-SA 4.0
2023-08-25T19:56:39.930Z
@mousetail That wouldn't have the desired side-effects, though.
kaya3
CC BY-SA 4.0
2023-08-26T14:59:37.637Z
You can do the same thing with the comma operator `javascript:document.body.style.backgroundColor='green',undefined`
Paul Draper
CC BY-SA 4.0
2023-08-26T16:46:48.417Z
or with `&& undefined`
njzk2
CC BY-SA 4.0
2023-08-26T16:48:02.447Z
"Other proposed uses include it being a reliable way to get an undefined value before the keyword undefined was added to the language" are you saying that `void` predates the `undefined` keyword (and that `undefined` value also predates the `undefined` keyword?
njzk2
CC BY-SA 4.0
2023-08-26T19:02:48.093Z
@njzk2 Yes; Netscape Navigator 2.0 was released in November 1995, so `void` existed in Javascript by then. But `undefined` was only made a global constant in ES5, which came out in 2009. Incidentally, the name of the language was changed (from LiveScript to JavaScript) in December 1995, so you *could* say that the `void` operator has always been in JavaScript. ([source](https://web.archive.org/web/20200227184037/https://speakingjs.com/es5/ch04.html))
kaya3
CC BY-SA 4.0
2023-08-28T01:08:48.513Z
JavaScript has no `undefined` keyword. It has a _globally-defined variable_ of that name, of type `undefined`, but nothing stops you from writing `let undefined = 42;` in a more restricted scope.
Longinus
CC BY-SA 4.0
2023-08-28T16:43:54.997Z
@Longinus in previous versions, nothing stopped you from doing that in the global scope either.
OrangeDog
CC BY-SA 4.0
2023-08-28T18:15:25.500Z
I think the cited historical reason should be first in this answer, and the other practical uses later.
user3840170
# Why did JavaScript choose to include a void operator?
Posted by: username_0 on 2023-08-25
In JavaScript, there is an operator void that, given an expression, evaluates it but then ignores the result and evaluates to undefined:
let a = void 1+1;
console.log(a);
// undefined

The use case for void, at least nowadays, is extremely niche. void 1 is a slightly shorter way of writing undefined which helps save a few bytes of bandwidth. It's also used to explicitly discard the result of functions you are calling for their side effects.
Both of these can be very easily emulated using other language features though. I know of no other language that has an operator like this. Why did JavaScript include void?
Ideally, answers would cite real references and refrain from baseless speculation. I'm specifically looking for the reason it was added in the first place, not modern-day uses.
**Tags:** history javascript
> **Comments:**
> **username_1**: [This](https://stackoverflow.com/questions/666936/what-is-the-point-of-void-operator-in-javascript) thread might provide some answers.  
> **username_2**: _"I know of no other language that has an operator like this"_ — in C you can write `(void)foo(bar);`, which would ignore the return value of `foo` and (usually) suppress a diagnostic about unused value.  
> **username_0**: @Ruslan That's really just a special case of casting though  
> **username_2**: Yes, but this special case is explicitly spelled out in the Standard, see [this answer](https://stackoverflow.com/a/34289000/673852). (oh, it's about C++ actually)  
> **username_3**: "*A special case of casting*": yes, and what? `(void)` is an operator in C, both semantically, and in the terminology of the language spec.  It has an effect substantially analogous to JS's `void` operator.  How is it relevant that C also has other cast operators?  
---
## 1 answers
### Answer by username_4 on 2023-08-25. Score: 44 | Accepted answer
The most compelling reason for having it in the language, seems to be for its use in javascript: URLs, such as in the href attribute of an  tag (example from this MDN article):

  Click here for green background


The code in a javascript: URL is evaluated as an expression, and the result ─ if it is not undefined ─ is used (as a string), as the link URL in this case. But here, the assignment expression produces the string 'green', which is not supposed to be a link address; so the void operator is needed to discard that string, so the browser doesn't navigate away from the page when the link is clicked.
The same applies to javascript: links loaded directly in the address bar, from the browser's bookmarks or history, or otherwise. Discarding the values produced by these links is confirmed as the actual reason for having the void operator, in an email from Javascript creator Brendan Eich (web.archive.org link):

In particular, I made it possible to generate a new document by loading, e.g. javascript:'hello, world', but also (key for bookmarklets) to run arbitrary script against the DOM of the current document, e.g. javascript:alert(document.links[0].href). The difference is that the latter kind of URL use an expression that evaluates to the undefined type in JS. I added the void operator to JS before Netscape 2 shipped to make it easy to discard any non-undefined value in a javascript: URL.


MDN suggests a couple other possible uses for the void operator, but they aren't so compelling:

It allows an IIFE to be used where a statement would be expected; but parentheses also work for this.
It allows discarding the result of an arrow function when a callback should be provided which doesn't leak a return value; but braces around the body of the arrow function also work for this.

Other proposed uses include it being a reliable way to get an undefined value before the global constant undefined was added to the language, and it being a way to silence linter warnings about unused results of some operations.
> **Comments:**
> **username_5** (2023-08-25): It can also be used to make a linter shut up about fire-and-forget promises, but most of the time you at least want `.catch(() => {})` to prevent unhandled rejection errors.  
> **username_0** (2023-08-25): `javascript:undefined` works for a href too though. And most of your other reasons relate to newer uses that didn't exit when `void` was first added  
> **username_4** (2023-08-25): @mousetail That wouldn't have the desired side-effects, though.  
> **username_6** (2023-08-26): You can do the same thing with the comma operator `javascript:document.body.style.backgroundColor='green',undefined`  
> **username_7** (2023-08-26): or with `&& undefined`  
> **username_7** (2023-08-26): "Other proposed uses include it being a reliable way to get an undefined value before the keyword undefined was added to the language" are you saying that `void` predates the `undefined` keyword (and that `undefined` value also predates the `undefined` keyword?  
> **username_4** (2023-08-26): @njzk2 Yes; Netscape Navigator 2.0 was released in November 1995, so `void` existed in Javascript by then. But `undefined` was only made a global constant in ES5, which came out in 2009. Incidentally, the name of the language was changed (from LiveScript to JavaScript) in December 1995, so you *could* say that the `void` operator has always been in JavaScript. ([source](https://web.archive.org/web/20200227184037/https://speakingjs.com/es5/ch04.html))  
> **username_8** (2023-08-28): JavaScript has no `undefined` keyword. It has a _globally-defined variable_ of that name, of type `undefined`, but nothing stops you from writing `let undefined = 42;` in a more restricted scope.  
> **username_9** (2023-08-28): @Longinus in previous versions, nothing stopped you from doing that in the global scope either.  
> **username_10** (2023-08-28): I think the cited historical reason should be first in this answer, and the other practical uses later.  
---
