Skip to content

useValidCharacterComparison (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useValidCharacterComparison": "error"
}
}
}
}

Disallow comparing a single character with a string that has more than one character.

charAt() returns a string that has one character at most. The same is true when you read a character from a string with at() or square brackets, like text[0]. Comparing that result with a longer string with ===, ==, !==, or != always gives the same answer, so the comparison is likely a mistake.

JavaScript counts some characters, such as most emojis, as two characters in a string. For example, "😀".length is 2, so text.charAt(0) can never be equal to "😀".

Arrays also have at() and square-bracket access, and their items can be longer strings. For this reason, the rule checks at() and square brackets only when the value is clearly a string: a string literal, or a variable declared with the string type or with a const string literal.

text.charAt(4) === "a2";
code-block.js:1:1 lint/nursery/useValidCharacterComparison ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This comparison is always false.

> 1 │ text.charAt(4) === "a2";
│ ^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ This expression returns one character at most.

> 1 │ text.charAt(4) === "a2";
│ ^^^^^^^^^^^^^^
2 │

ℹ But this string has more than one character.

> 1 │ text.charAt(4) === "a2";
│ ^^^^
2 │

ℹ Compare the character with a string of one character, or use a method such as startsWith() to check for longer text.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

text.charAt(0) !== "/n";
code-block.js:1:1 lint/nursery/useValidCharacterComparison ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This comparison is always true.

> 1 │ text.charAt(0) !== "/n";
│ ^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ This expression returns one character at most.

> 1 │ text.charAt(0) !== "/n";
│ ^^^^^^^^^^^^^^
2 │

ℹ But this string has more than one character.

> 1 │ text.charAt(0) !== "/n";
│ ^^^^
2 │

ℹ Compare the character with a string of one character, or use a method such as startsWith() to check for longer text.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

"abc".at(0) === "ab";
code-block.js:1:1 lint/nursery/useValidCharacterComparison ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This comparison is always false.

> 1 │ "abc".at(0) === "ab";
│ ^^^^^^^^^^^^^^^^^^^^
2 │

ℹ This expression returns one character at most.

> 1 │ "abc".at(0) === "ab";
│ ^^^^^^^^^^^
2 │

ℹ But this string has more than one character.

> 1 │ "abc".at(0) === "ab";
│ ^^^^
2 │

ℹ Compare the character with a string of one character, or use a method such as startsWith() to check for longer text.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const text = "abc";
text[0] === "ab";
code-block.js:2:1 lint/nursery/useValidCharacterComparison ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This comparison is always false.

1 │ const text = "abc";
> 2 │ text[0] === "ab";
│ ^^^^^^^^^^^^^^^^
3 │

ℹ This expression returns one character at most.

1 │ const text = "abc";
> 2 │ text[0] === "ab";
│ ^^^^^^^
3 │

ℹ But this string has more than one character.

1 │ const text = "abc";
> 2 │ text[0] === "ab";
│ ^^^^
3 │

ℹ Compare the character with a string of one character, or use a method such as startsWith() to check for longer text.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

text.charAt(4) === "a";
text.charAt(4) === "\n";
text.charAt(4) === "";
text.startsWith("a2");