Replace void 0 with undefined

Replace void 0 and other constant void expressions with undefined.

Replace void 0 with undefined

This refactoring replaces void 0 and other constant void expressions without side-effects with undefined (unless undefined is declared as a local variable).

Why is this refactoring helpful?

In JavaScript, undefined is not a reserved keyword. Instead, it is a property of the global object. void 0 was commonly used before ES5 to make the code failsafe against the re-definition of the undefined global property because the void operator always returns the true global undefined. With ES5, undefined became a read-only global property.

In modern browsers and JavaScript engines, guarding against the re-definition of the undefined global property has become an unnecessary complication. Using only undefined makes the code easier to understand, particularly for developers with less JavaScript experience.

What do I need to consider?

Undefined can be shadowed by a local variable

While changing the undefined global property is no longer possible, undefined can still be shadowed by a local variable:

const undefined = "i am not the real undefined";
let check = aVariable === void 0; // void 0 is needed here

However, this is not a best practice, and having local variables with the name undefined should be avoided.

The 'undefined' global property can be redefined in ancient browsers

ES5 is from 2009, and all modern browsers and JavaScript environments prevent changes to the undefined global property. However, this is not the case for ancient browsers such as Internet Explorer 8.

Configuration

Learn More

Available In