The void operator evaluates its argument and always returns undefined. void operator allows using any
expression in the places where an undefined is expected. However, using void makes code more difficult to understand, as the
intent is often not clear.
void doSomething(); // Noncomplaint
In ECMAScript5 and newer environments, the global property undefined cannot be reassigned, so using void for this reason
should also be avoided.
undefined = 'Hello, world!'; // since ECMAScript5, this statement has no effect
if (parameter === void 42) { // Noncompliant
// ...
}
void 0 was a conventional way to obtain the undefined value, so it’s still
accepted by the rule.
if (parameter === void 0) { // Compliant
// ...
}
void is also allowed with immediately invoked function expressions, as otherwise parsing would fail.
void function() { // Compliant
// ...
}();
void is allowed with Promise-like objects. const promise = new Promise((resolve, reject) => resolve(42)); void promise;
When using void … to get the undefined value, directly use the undefined identifier instead.
if (parameter === void 42) { // Noncompliant
// ...
}
if (parameter === undefined) {
// ...
}
Otherwise, the void operator should be removed to avoid confusion for maintainers.
doSomethingElse(void doSomething()); // Noncompliant
doSomething(); doSomethingElse();