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 expressionorvoid(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 0is also a reliable way to obtainundefinedwhere the nameundefineditself could have been shadowed.
Quick example
void 123; // undefined
void 'hello'; // undefined
void (5 + 5); // undefinedIt 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:
function sayHi() {
console.log('Hello!');
return 'Hi';
}
console.log(sayHi()); // "Hi"
console.log(void sayHi()); // runs, but returns undefinedHere 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
1. To make a link go nowhere
Sometimes a click on a link should do nothing:
<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:
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
const log = msg => void console.log(msg);
log('Hello'); // prints to the console, but returns undefinedHere 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: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
function calc() {
return 42;
}
console.log(calc()); // 42
console.log(void calc()); // undefinedThe function runs in both cases, but void forces the expression to return undefined.
| Property | Value |
|---|---|
| What it does | Evaluates the expression and returns undefined |
| What it returns | undefined |
| What it is for | Ignoring the result of an expression |
| Common scenarios | javascript:void(0) links, IIFEs, arrow functions without a return |
Common mistakes
- Thinking
voidcancels 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>orevent.preventDefault(). - Confusing
voidwithundefinedas 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 + bparses as(void a) + b, so parentheses are mandatory in compound expressions. - Applying
voidto an async function and assuming errors are gone. The promise still runs, and an unhandled rejection does not disappear.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.