Member-only story
What is “void 0” in JavaScript?
Is it similar to undefined? Should we avoid it or not?
Hi, I’m a software engineer, and welcome to my blog. Today, we are going to discuss void 0.
What we should know about void 0
In older versions of JavaScript, you wouldn’t have found void 0 expressions. The keyword is void which is undefined.
void 0
void (0)
void "hello"
void (new Date())
//all will return undefined
undefined is mutable
So why do we use void 0 as an alias for undefined?
One of JavaScript’s quirks is that it is not a reserved keyword. Instead, it is a property of the global object.
According to wtjs.com,
Undefined is nothing but a global variable name without a default value. Therefore, its primitive value is undefined. You can change the value of undefined:
var a = {};
a.b === undefined; // true because property b is not set
undefined = 42;
a.b === undefined; // false