Skip to main content

The void operator

The void operator evaluates an expression and always returns undefined, whatever the expression itself produces. It is a rare but useful tool when you need to run something and guarantee that nothing comes back.

Theory

TL;DR

  • Syntax: void expression or void(expression).
  • The expression really is evaluated, so all of its side effects happen.
  • The result is always undefined, independently of the expression.
  • Classic scenarios: javascript:void(0) links, bookmarklets, IIFEs, arrow functions that must not return a value.
  • void 0 is also a reliable way to obtain undefined where the name undefined itself could have been shadowed.

Quick example

javascript
void 123; // undefined void 'hello'; // undefined void (5 + 5); // undefined

It does not matter what the expression on the right evaluates to: the result of void is always undefined.

How void works

The operator runs the expression but always returns undefined. The function is genuinely called:

javascript
function sayHi() { console.log('Hello!'); return 'Hi'; } console.log(sayHi()); // "Hi" console.log(void sayHi()); // runs, but returns undefined

Here sayHi() is called and prints to the console, but its result 'Hi' is ignored and undefined comes out instead.

There is also a historical reason for the operator: in old code undefined was not a reserved word and could be shadowed inside a scope. Writing void 0 always produced the real undefined.

Where it is used in practice

Sometimes a click on a link should do nothing:

html
<a href="javascript:void(0)">Click me</a>

Here void(0) is evaluated, the result is undefined, and the browser does not navigate anywhere because href yields no address.

This is an outdated practice: modern code should use href="#" together with event.preventDefault(), or just a <button>.

2. For IIFEs

void used to be placed before a function to guarantee that an IIFE executed correctly:

javascript
void function() { console.log('It ran!'); }();

Without void the parser could read the expression as a function declaration. Today this is rarely needed, but historically void served exactly as that safety net.

3. In arrow functions, to return nothing

javascript
const log = msg => void console.log(msg); log('Hello'); // prints to the console, but returns undefined

Here void is used to perform an action without returning a value. This is handy when an API expects a callback that must return undefined rather than some accidental value.

4. In bookmarklets, the mini scripts stored in bookmarks

javascript
javascript:void(alert('Hello'))

Without void the browser would try to navigate to a "page" holding the result of the expression, and void prevents that.

Compared with a plain call

javascript
function calc() { return 42; } console.log(calc()); // 42 console.log(void calc()); // undefined

The function runs in both cases, but void forces the expression to return undefined.

PropertyValue
What it doesEvaluates the expression and returns undefined
What it returnsundefined
What it is forIgnoring the result of an expression
Common scenariosjavascript:void(0) links, IIFEs, arrow functions without a return

Common mistakes

  • Thinking void cancels execution. The expression is evaluated in full, only its value is discarded.
  • Using javascript:void(0) in new code. For an action without navigation use a <button> or event.preventDefault().
  • Confusing void with undefined as a type. It is an operator, not a type; the similarly named type belongs to TypeScript and means something else.
  • Forgetting operator precedence. void a + b parses as (void a) + b, so parentheses are mandatory in compound expressions.
  • Applying void to an async function and assuming errors are gone. The promise still runs, and an unhandled rejection does not disappear.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.