All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S2692.html Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version

Most checks against an indexOf call against an array compare it with -1 because 0 is a valid index. Any checks which look for values >0 ignore the first element, which is likely a bug. If you’re merely checking the presence of the element, consider using includes instead. Before using includes method make sure that your browser version is supporting it.

Noncompliant Code Example

var color = "blue";
var name = "ishmael";

var arr = [color, name];

if (arr.indexOf("blue") > 0) { // Noncompliant
  // ...
}

Compliant Solution

var color = "blue";
var name = "ishmael";

var arr = [color, name];

if (arr.indexOf("blue") >= 0) {
  // ...
}
if (arr.includes("blue")) {
  // ...
}

See

Array.prototype.includes() documentation at MDN





© 2015 - 2024 Weber Informatics LLC | Privacy Policy