Skip to content

noConstantCondition (since v1.0.0)

Diagnostic Category: lint/correctness/noConstantCondition

Sources:

Disallow constant expressions in conditions

if (false) {
doSomethingUnfinished();
}
correctness/noConstantCondition.js:1:5 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━

   Unexpected constant condition.
  
  > 1 │ if (false) {
       ^^^^^
    2 │     doSomethingUnfinished();
    3 │ }
  
if (Boolean(1)) {
doSomethingAlways();
}
correctness/noConstantCondition.js:1:5 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━

   Unexpected constant condition.
  
  > 1 │ if (Boolean(1)) {
       ^^^^^^^^^^
    2 │     doSomethingAlways();
    3 │ }
  
if (undefined) {
doSomethingUnfinished();
}
correctness/noConstantCondition.js:1:5 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━

   Unexpected constant condition.
  
  > 1 │ if (undefined) {
       ^^^^^^^^^
    2 │     doSomethingUnfinished();
    3 │ }
  
for (;-2;) {
doSomethingForever();
}
correctness/noConstantCondition.js:1:7 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━

   Unexpected constant condition.
  
  > 1 │ for (;-2;) {
         ^^
    2 │     doSomethingForever();
    3 │ }
  
while (typeof x) {
doSomethingForever();
}
correctness/noConstantCondition.js:1:8 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━

   Unexpected constant condition.
  
  > 1 │ while (typeof x) {
          ^^^^^^^^
    2 │     doSomethingForever();
    3 │ }
  
var result = 0 ? a : b;
correctness/noConstantCondition.js:1:14 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━

   Unexpected constant condition.
  
  > 1 │ var result = 0 ? a : b;
                ^
    2 │ 
  
if (x === 0) {
doSomething();
}
for (;;) {
doSomethingForever();
}
while (typeof x === "undefined") {
doSomething();
}
do {
doSomething();
} while (x);
var result = x !== 0 ? a : b;
// Exception
while (true) {
if (x) { break; }
x = f();
}