Result of String.prototype.toLowerCase() and String.prototype.toUpperCase() should be compared with a lower and upper case string

  • BAD_STRING_CASE_COMPARISON
  • Error
  • Medium
  • No tags

This rule applies when String.prototype.toLowerCase() or String.prototype.toUpperCase() result is compared with a different case string.

String.prototype.toLowerCase() or String.prototype.toUpperCase() returns a lower or upper case string.

Therefore, when it is compared with a different lower or upper case string, the result will be always false or true. This is not likely to be a programmer's intent.

Noncompliant Code Example

View with compliant examples side by side
// Example 1
var str = "foo";
if (str.toUpperCase() === "Foo") { // BAD_STRING_CASE_COMPARISON alarm because "Foo" is a non-uppercase string.
    console.log("is FOO");
}

// Example 2
var bar = "bar";
if (bar.toUpperCase().startsWith("b")) { // BAD_STRING_CASE_COMPARISON alarm because "b" is a non-uppercase string.
    console.log("starts with b");
}

Compliant Code Example

View with noncompliant examples side by side
// Example 1
var str = "foo";
if (str.toUpperCase() === "FOO") {
    console.log("is FOO");
}

// Example 2
var bar = "bar";
if (bar.toUpperCase().startsWith("B")) {
    console.log("starts with B");
}

Version

This rule was introduced in DeepScan 1.8.0-beta.

See

Was this documentation helpful?