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

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

There is a newer version: 10.17.0.28100
Show newest version

Why is this an issue?

The use of comparison operators (<, <=, >=, >) with strings is not likely to yield the expected results. Make sure the intention was to compare strings and not numbers.

Noncompliant code example

var appleNumber = "123";
var orangeNumber = "45";
if (appleNumber < orangeNumber) {  // Noncompliant, this condition is true
  alert("There are more oranges");
}

Compliant solution

var appleNumber = "123";
var orangeNumber = "45";
if (Number(appleNumber) < Number(orangeNumber)) {
  alert("There are more oranges");
}

Exceptions

The rule ignores string comparisons occurring in the callback of a sort invocation, e.g.:

const fruits = ['orange', 'apple', 'banana'];
fruits.sort((a, b) => a < b);




© 2015 - 2024 Weber Informatics LLC | Privacy Policy