38 lines
1.2 KiB
Plaintext
38 lines
1.2 KiB
Plaintext
![]() |
In JavaScript, we can use the ``++/++`` (backslash) character to escape special characters inside a string, a template literal, or a regular expression.
|
||
|
|
||
|
It indicates that the next character should be treated as a literal character rather than as a special character or string delimiter.
|
||
|
|
||
|
For instance, it is common to escape single quotes inside a string literal using the single quote delimiter like ``++'It\'s a beautiful day'++``.
|
||
|
|
||
|
Escaping is only meaningful for special characters.
|
||
|
|
||
|
== Why is this an issue?
|
||
|
|
||
|
Escaping non-special characters in strings, template literals, and regular expressions doesn't affect their value. Therefore, useless escapes impact code readability and could even denote a bug in the code if the developer left it by mistake or intended to escape another special character instead.
|
||
|
|
||
|
== How to fix it
|
||
|
|
||
|
We can safely remove a useless character escape without changing the original behaviour.
|
||
|
|
||
|
=== Code examples
|
||
|
|
||
|
==== Noncompliant code example
|
||
|
|
||
|
[source,javascript]
|
||
|
----
|
||
|
const regex = /[\[]/;
|
||
|
const octal = '\8';
|
||
|
const hello = 'Hello, world\!';
|
||
|
const path = `\/${some}\/${dir}`;
|
||
|
----
|
||
|
|
||
|
==== Compliant solution
|
||
|
|
||
|
[source,javascript]
|
||
|
----
|
||
|
const regex = /[[]/;
|
||
|
const octal = '8';
|
||
|
const hello = 'Hello, world!';
|
||
|
const path = `/${some}/${dir}`;
|
||
|
----
|