Skip to content

Files

Latest commit

 

History

History
25 lines (19 loc) · 506 Bytes

bad-char-at-comparison.md

File metadata and controls

25 lines (19 loc) · 506 Bytes

Pattern: Invalid charAt comparison

Issue: -

Description

The charAt method always returns a single character string. Comparing its result with a string longer than one character will always evaluate to false and indicates a logic error.

Examples

Example of incorrect code:

if (str.charAt(0) === "ab") {
  // Will never be true
}
str.charAt(5) === "\r\n";

Example of correct code:

if (str.charAt(0) === "a") {
  // Can be true
}
str.charAt(5) === "\n";