Discard a computed result
A callback or expression must run for its side effects, while the surrounding expression should receive undefined.
JS Puzzles · Const
The void operator evaluates an expression, discards its result, and returns undefined. Parenthesize complex operands to make operator precedence explicit.
The unary void operator evaluates one expression, ignores the value that expression produces, and yields undefined. Parentheses around the operand are optional, but they help prevent precedence misunderstandings when the operand contains an operator such as a comparison.
void expression| Name | Type | Description |
|---|---|---|
expression | JavaScript expression | The JavaScript expression to evaluate; its resulting value is discarded. |
Always returns undefined.
const result = void console.log("side effect");
console.log(result); // undefined
console.log(void (2 === "2")); // undefined
console.log(void 2 === "2"); // falseThe first expression runs the logging call but converts its result to undefined. Parentheses make it clear whether void applies to the comparison or only to the number.
A callback or expression must run for its side effects, while the surrounding expression should receive undefined.
A compact expression needs to produce the undefined primitive explicitly.
A complex operand contains comparisons or other operators whose grouping could be misread.
void evaluates its operand and always returns undefined. The operand can still perform side effects, because evaluation happens before its value is discarded.
Follow-up: What does void (2 === "2") return? It returns undefined, after evaluating the comparison.
The operand is a JavaScript expression, and parentheses around it are optional. The operator's result is always undefined regardless of the operand's result.
Follow-up: What does void 2 === "2" evaluate to? It is (void 2) === "2", so it evaluates to false.
void 0 and void(0) both produce undefined. The operator is also useful when an expression must be evaluated for its effects but its resulting value should not be used.
Follow-up: Why can parentheses matter? They control whether void applies before or after another operator, avoiding precedence ambiguity.
undefinedUnary operatorsExpressions and operators