Skip to content

Commit 44109e4

Browse files
author
James Allardice
committed
'unneccessary escapement' article
1 parent a51dfee commit 44109e4

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

articles/unnecessary-escapement.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!---
2+
{
3+
"titles": [
4+
"Unnecessary escapement",
5+
"Unexpected '\\'"
6+
],
7+
"slugs": [
8+
"unnecessary-escapement",
9+
"unexpected-backslash"
10+
],
11+
"linters": [
12+
"jslint"
13+
],
14+
"author": "jallardice"
15+
}
16+
-->
17+
18+
### History
19+
20+
This warning has existed in two forms in JSLint. It was introduced in the
21+
original version and has remained ever since. It is not present in JSHint or
22+
ESLint.
23+
24+
- In JSLint versions dated December 2010 and earlier the warning given is
25+
"Unnecessary escapement"
26+
27+
- In more recent versions the message has changed to "Unexpected '\'"
28+
29+
The situations that produce the warning have not changed despite changes to the
30+
text of the warning itself.
31+
32+
### When do I get this error?
33+
34+
The "Unnecessary escapement" error (and the alternative "Unexpected '\') is
35+
thrown when JSLint encounters **a string containing a unicode or hexadecimal
36+
escape sequence that could be replaced with the literal character**. In the
37+
following example we use the unicode escape sequence for the | (vertical line)
38+
character:
39+
40+
<!---
41+
{
42+
"linter": "jslint"
43+
}
44+
-->
45+
```javascript
46+
var title = "My Website \u007c Welcome";
47+
```
48+
49+
### Why do I get this error?
50+
51+
This error is raised to highlight a **potentially confusing piece of code**.
52+
When the character in question is safely represented without an escape sequence
53+
it is generally preferrable not to use an escape sequence because it makes the
54+
code easier to read. It would be difficult to know exactly what character is
55+
represented by `\u007c` in the above example without looking it up.
56+
57+
The error is easily resolved by simply replacing unnecessary escape sequences
58+
with the appropriate characters:
59+
60+
<!---
61+
{
62+
"linter": "jslint"
63+
}
64+
-->
65+
```javascript
66+
var title = "My Website | Welcome";
67+
```

0 commit comments

Comments
 (0)