Better Programming

Advice for programmers.

Follow publication

You Don’t Need void 0 in JavaScript

Lars Grammel
Better Programming
Published in
3 min readMay 10, 2022

Why is void 0 used as an alias for undefined?

// Pre-ES5 example - does not work in modern JS engines:// changes property 'undefined' on the global object:
undefined = "something else";
// potentially in some other JavaScript file or script section:
if (aVariable === undefined) {
// aVariable is equal to "something else",
// but not to the primitive value 'undefined'
doSomething();
}

The global property ‘undefined’ after ES5

globalThis.undefined = "something else";
console.log(undefined); // prints undefined in modern browsers

Undefined can still be shadowed by a local variable

const undefined = "something else";
let check = aVariable === void 0; // void 0 is needed here

Can void 0 help reduce the bundle size?

Avoid void 0 in modern JavaScript

Lars Grammel
Lars Grammel

Written by Lars Grammel

AI Engineering consulting and contract work. x.com/lgrammel

Responses (1)

Write a response