Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Operators

The void operator

Reference for the JavaScript void operator: evaluating an expression and returning undefined, void 0, javascript: URLs and discarding return values.

The void operator evaluates an expression and then throws the result away, always returning undefined. It is rare in modern code, but you still see it in minified output (void 0 is a short way to write undefined) and in places where you want to be sure a value is ignored.

JavaScript
void expression

Try it

JS Always undefined

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Discarding a return value

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
void expr
Operand Description
expr Any expression. It is evaluated for its side effects.

Return value: always undefined.

Exceptions: none of its own. If evaluating expr throws, the error propagates.

Usage notes

  • void 0 versus undefined: in old scripts undefined could be reassigned inside a function, so void 0 was the safe way to get the real value. Today undefined is read-only at the global level and a shadowed local named undefined is unusual, so write undefined unless you are minifying.
  • Precedence is high: void 1 + 2 is (void 1) + 2, which is NaN. Use parentheses: void (1 + 2).
  • Ignoring promises: void fetchData() tells readers (and some linters, for example a "no floating promises" rule) that you deliberately aren't awaiting the result. It does not handle rejections: add a .catch() if the promise can fail.
  • Arrow functions: () => void doSomething() makes sure a callback returns nothing, which matters when a caller treats a returned value specially (for example, a returned function in some frameworks).
  • javascript: URLs: href="javascript:void(0)" stops the browser from navigating to the result of the expression. It is an old pattern; use a <button> instead, and avoid javascript: URLs because they are blocked by many content security policies.
  • Statements: void needs an expression. It can't wrap an if or a loop.
  • Not the same as typeof: typeof x returns a string, void x always undefined.

Browser support

Supported in every browser and runtime.