Hiprup

What is void 0 and why was it used?

void evaluates an expression and always returns undefined. void 0 is a reliable way to produce undefined.

  • Why historically — in old code undefined could be reassigned, so void 0 guaranteed the real value.

  • Other uses — void in javascript: links to prevent navigation.

Rarely needed now: undefined can't be reassigned in modern scopes, so just use undefined.

console.log(void 0);           // undefined
console.log(void 'anything');  // undefined
console.log(void 0 === undefined); // true

// Historical use: safe undefined
// In old JS, this was possible:
// var undefined = 'oops';
// console.log(undefined); // 'oops' — not actual undefined!
// void 0 always returns real undefined

// Prevent link navigation
// <a href="javascript:void(0)" onclick="doSomething()">Click</a>
// Better: <button onclick="doSomething()">Click</button>

// In arrow functions (prevent returning a value)
const sideEffect = () => void doSomething();
// Ensures undefined is returned, not doSomething's return value

void evaluates its operand and returns undefined. void 0 is the most common form. In modern JS (ES5+), undefined is read-only globally, making void 0 unnecessary.

The arrow function use (void doSomething()) prevents accidental return values from one-liner side effects.

void 0 = safe undefined (before ES5, undefined could be overridden). In modern JS, undefined is read-only — void 0 is unnecessary but still appears in minified code and some patterns.

The arrow function trick (void fn()) prevents accidental returns.

What is void 0 and why was it used? | Hiprup