Skip to content

charAtComparisons

Reports comparing charAt() results with strings longer than one character.

✅ This rule is included in the ts logical presets.

The charAt method returns a string of exactly one character. If the return value is compared with a string of length greater than one, the comparison will always evaluate to the same result. Equality comparisons (===, ==) will always be false, and inequality comparisons (!==, !=) will always be true.

text.charAt(0) === "ab";
"abc" === text.charAt(0);
text.charAt(0) !== "hello";

This rule is not configurable.

This rule catches a specific programming error with no legitimate use cases. There is no reason to disable this rule.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.