JavaScript void keyword
last modified April 16, 2025
In this article we explore the void
keyword in JavaScript.
The void operator evaluates an expression and returns undefined.
The void keyword
The void
operator evaluates any given expression and then
returns undefined. It is a unary operator that precedes its operand.
The operand can be of any type. The void operator is rarely used in
modern JavaScript.
The primary use of void is to obtain the undefined primitive value. This is useful when you want to ensure an expression has no return value. It can also prevent unwanted behavior in certain contexts.
Void is commonly seen in older JavaScript code, particularly with javascript: URLs. Modern JavaScript practices have reduced its usage. However, understanding void remains important for legacy code.
Basic void usage
The simplest use of void is to evaluate an expression and return undefined.
const result = void(5 + 3); console.log(result);
Here, void evaluates the expression (5 + 3) but discards the result. The variable result gets assigned undefined. The calculation happens but the result isn't used.
$ node main.js undefined
Void with function calls
Void can be used to call functions while ignoring their return values.
function greet() { console.log('Hello!'); return 'Greeting complete'; } const output = void greet(); console.log(output);
The greet function executes and logs 'Hello!', but its return value is discarded. The output variable receives undefined from the void operator.
$ node main.js Hello! undefined
Void in href attributes
A traditional use of void was in javascript: URLs to prevent navigation.
<a href="javascript:void(0)" onclick="alert('Clicked!')"> Click me </a>
This prevents the page from navigating when the link is clicked. The void(0) returns undefined, which stops the default link behavior. The onclick handler still executes normally.
Void with IIFEs
Void can be used with Immediately Invoked Function Expressions (IIFEs).
void function() { console.log('IIFE executed'); }();
This pattern ensures the function's return value (if any) is discarded. The void operator makes it clear the IIFE's purpose is side effects only. This is an alternative to wrapping in parentheses.
$ node main.js IIFE executed
Void vs undefined
Void always returns undefined, but it's not the same as using undefined directly.
const a = void 0; const b = undefined; console.log(a === b); console.log(typeof a); console.log(typeof b);
Both variables contain undefined and are equal, but void is an operator while undefined is a primitive value. In modern JS, using undefined directly is preferred over void 0.
$ node main.js true undefined undefined
Void in arrow functions
Void can be used with arrow functions to ensure they return undefined.
const logMessage = (msg) => void console.log(msg); const result = logMessage('Hello void'); console.log(result);
The arrow function logs the message but returns undefined due to void. This pattern explicitly indicates the function shouldn't return a value. It's clearer than just calling console.log without void.
$ node main.js Hello void undefined
Void with async functions
Void can be used with async functions to ignore their promises.
async function fetchData() { // Simulate API call return { data: 'Sample data' }; } void fetchData().then(() => console.log('Data fetched')); console.log('Continuing execution');
Here, void is used to execute the async operation without handling the promise. The code continues execution immediately. This pattern is useful for fire-and-forget async operations.
$ node main.js Continuing execution Data fetched
Source
In this article we have demonstrated how to use the void keyword in JavaScript to evaluate expressions without return values.