Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

void operator

The void operator evaluates an expression, discards its result, and returns undefined. Parenthesize complex operands to make operator precedence explicit.

How it works

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.

Syntax

void expression

Parameters

NameTypeDescription
expressionJavaScript expressionThe JavaScript expression to evaluate; its resulting value is discarded.

Returns

Always returns undefined.

Key points

  • It first evaluates the operand and then produces the undefined primitive.
  • Use it when evaluation should occur but the surrounding context requires an undefined result.
  • The parenthesized and unparenthesized zero forms are equivalent ways to create undefined.
  • Its return value does not depend on what the operand evaluates to.

Common mistakes

  • Without parentheses, void binds to its immediate operand: void 2 === "2" means (void 2) === "2", not void (2 === "2").
  • Using void merely to obtain undefined is usually unnecessary in ordinary source code; the global undefined value is available for that purpose.

Example: Evaluate, then discard

const result = void console.log("side effect");
console.log(result); // undefined

console.log(void (2 === "2")); // undefined
console.log(void 2 === "2"); // false

The 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.

When to use it

Discard a computed result

A callback or expression must run for its side effects, while the surrounding expression should receive undefined.

Produce undefined

A compact expression needs to produce the undefined primitive explicitly.

Clarify precedence

A complex operand contains comparisons or other operators whose grouping could be misread.

Interview questions

QWhat does the void operator do?

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.

QWhat is the syntax and return value of void?

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.

QWhen might code use void?

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.

  • undefined
  • Unary operators
  • Expressions and operators

Source: void operator - JavaScript | MDN. Checked 2026-10-15.