# no-constant-condition
Disallow constant conditions which always yield one result.
Constant conditions such as if (true) {}
are almost always a mistake. Constant
conditions always yield a single result which almost always ends up in unwanted behavior.
This rule is aimed at catching those conditions in if
, do while
, while
, and for
statements, as well as
conditional expressions.
# Incorrect Code Examples
if (true) {
// ^ this block is always used
} else {
//^^^^ this else block is unreachable
}
// This loop endlessly runs
for(foo = 5; 5; foo++) {
}
# Correct Code Examples
if (foo) {
/* */
}
More incorrect examples
if(6) {}
if(6 - 7 || 3 ? 7 && 2 : NaN + NaN || 2) {}
if (true) {}
if (NaN) {} else {}
6 + 2 ? false : NaN
false ? false : false ? false : false
while (true) {}
do { /* */ } while (NaN ? NaN : true)
do { } while (NaN ? Infinity : true)
More correct examples
if (foo) {}
if (false > foo) {} else {}
if (foo ? NaN : Infinity) {}
do {} while (foo + 6)
for(var i = 5; foo; i++) {}