Void ( ) Operator
The void operator evaluates the expression after it and then returns undefined, whatever the expression produced. Any side effects of the expression still happen, but its value is discarded.
const result = void console.log('Saving draft');console.log(result);console.log(void 0);console.log(void 'anything at all');This prints:
Saving draftundefinedundefinedundefinedvoid is an operator, not a function, so the parentheses in void(0) only group the expression, as they would after a minus sign.
Discarding a return value
Section titled “Discarding a return value”An arrow function with an expression body returns the value of that expression. Where a callback should return nothing, void makes that explicit:
const log = [];
const addEntry = (text) => log.push(text);const addEntryQuietly = (text) => void log.push(text);
console.log(addEntry('Opened file'));console.log(addEntryQuietly('Closed file'));console.log(log);This prints:
1undefined[ 'Opened file', 'Closed file' ]push() returns the new length of the array, so the first function leaks a number to its caller while the second returns undefined. This matters when the caller acts on the return value, for example where returning false from an onclick handler cancels the click’s default action.
A similar use is a call to an async function whose promise is deliberately not awaited. Writing void saveDraft(); tells the reader, and linters that check for forgotten promises, that ignoring the promise was intended.
void 0 as undefined
Section titled “void 0 as undefined”void 0 is a short, reliable way to write undefined, and minifiers still emit it. In very old browsers the global undefined could be reassigned, and today a local variable can still be named undefined and hide the global one:
function check(value) { const undefined = 'surprise'; console.log(value === undefined); console.log(value === void 0);}
check(undefined);This prints:
falsetrueNobody should name a variable undefined, so in ordinary code plain undefined is clearer than void 0.
Links that do nothing
Section titled “Links that do nothing”Older pages used void in a link’s href, so that clicking the link ran some JavaScript without leaving the page:
<a href="javascript:void(0)" onclick="openMenu()">Menu</a>In a browser, clicking this link calls openMenu() and the page stays where it is, because a javascript: URL that produces undefined does not navigate anywhere. A <button type="button"> with an event listener does the same job properly, since it works with the keyboard and screen readers as a control rather than as a link, and it does not need javascript: URLs, which content security policies often block.
Precedence
Section titled “Precedence”void binds as tightly as other unary operators such as typeof and !, so it applies only to the operand next to it:
console.log(void 1 + 2);console.log(void (1 + 2));This prints:
NaNundefinedThe first line computes undefined + 2, which is NaN. Parentheses are needed to discard a whole expression.
Steve Griffith covers this topic in his video JavaScript void Operator.