Skip to content

noUselessEscapeInString (JavaScript)

biome.json
{
"linter": {
"rules": {
"suspicious": {
"noUselessEscapeInString": "error"
}
}
}
}

Disallow unnecessary escapes in string literals.

Escaping non-special characters in string literals doesn’t have any effect. Hence, they may confuse a reader.

const s = "\a";
code-block.js:1:13 lint/suspicious/noUselessEscapeInString  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The character doesn't need to be escaped.

> 1 │ const s = "\a";
^
2 │

Only quotes that enclose the string and special characters need to be escaped.

Safe fix: Unescape the character.

1 │ const·s·=·"\a";
-
const o = {
"\a": 0,
};
code-block.js:2:7 lint/suspicious/noUselessEscapeInString  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The character doesn't need to be escaped.

1 │ const o = {
> 2 │ "\a": 0,
^
3 │ };
4 │

Only quotes that enclose the string and special characters need to be escaped.

Safe fix: Unescape the character.

2 │ ····"\a":·0,
-
const s = `${0}\a`;
code-block.js:1:17 lint/suspicious/noUselessEscapeInString  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The character doesn't need to be escaped.

> 1 │ const s = `${0}\a`;
^
2 │

Only quotes that enclose the string and special characters need to be escaped.

Safe fix: Unescape the character.

1 │ const·s·=·`${0}\a`;
-
const s = "\n";

In template literals, \${ and $\{ are valid escapes:

const s = `\${0}`;

Tagged string templates are ignored:

const s = tagged`\a`;

JSX strings are ignored:

<div attr="str\a"/>;