29
$\begingroup$

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.

$\endgroup$
5
  • 1
    $\begingroup$ This thread might provide some answers. $\endgroup$ Aug 25, 2023 at 19:08
  • 7
    $\begingroup$ "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. $\endgroup$
    – Ruslan
    Aug 26, 2023 at 19:41
  • $\begingroup$ @Ruslan That's really just a special case of casting though $\endgroup$
    – mousetail
    Aug 26, 2023 at 19:42
  • 1
    $\begingroup$ Yes, but this special case is explicitly spelled out in the Standard, see this answer. (oh, it's about C++ actually) $\endgroup$
    – Ruslan
    Aug 26, 2023 at 19:43
  • 1
    $\begingroup$ "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? $\endgroup$ Aug 28, 2023 at 21:24

1 Answer 1

43
$\begingroup$

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 <a> tag (example from this MDN article):

<a href="javascript:void(document.body.style.backgroundColor='green');">
  Click here for green background
</a>

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.

$\endgroup$
10
  • 2
    $\begingroup$ 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. $\endgroup$
    – Bbrk24
    Aug 25, 2023 at 19:34
  • 1
    $\begingroup$ @mousetail That wouldn't have the desired side-effects, though. $\endgroup$
    – kaya3
    Aug 25, 2023 at 19:56
  • 2
    $\begingroup$ You can do the same thing with the comma operator javascript:document.body.style.backgroundColor='green',undefined $\endgroup$ Aug 26, 2023 at 14:59
  • 4
    $\begingroup$ @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) $\endgroup$
    – kaya3
    Aug 26, 2023 at 19:02
  • 3
    $\begingroup$ 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. $\endgroup$
    – Longinus
    Aug 28, 2023 at 1:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .