The void
operator evaluates its argument and always returns undefined
. void
allows using any expression
where an undefined
is expected. However, using void
makes code more difficult to understand, as the intent is often
unclear.
if (parameter === void 42) { // Noncompliant
// ...
}
doSomethingElse(void doSomething()); // Noncompliant
Instead of using void
to get the undefined
value, use the undefined
global property. In ECMAScript5 and
newer environments, undefined
cannot be reassigned. In other cases, remove the void
operator to avoid confusion for
maintainers.
if (parameter === undefined) {
// ...
}
doSomething();
doSomethingElse();
Exceptions
-
void 0
(or the equivalent void(0)
) is allowed as it was a conventional way to obtain the undefined
value
in environments before ECMAScript 5.
if (parameter === void 0) {
// ...
}
-
void
is allowed with immediately invoked function expressions.
void function() {
// ...
}();
const runPromise = () => Promise.resolve();
void runPromise();