org.sonar.l10n.javascript.rules.javascript.S3735.html Maven / Gradle / Ivy
Why is this an issue?
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() {
// ...
}();
-
void
is allowed with Promise-like objects to mark a promise as intentionally not awaited, as advised by @typescript-eslint/no-floating-promises.
const runPromise = () => Promise.resolve();
void runPromise();
Resources
Documentation
- MDN web docs -
void
operator
- MDN web docs -
undefined
- MDN web docs - IIFE (Immediately Invoked Function Expression)