2

You know that "n" and "\n" are not same in JavaScript, cause the second one is a escape sequence, but why "\a" and "a" is the same? If you check charCodeAt of the two strings, you will know. Can someone explain to me? What exactly escape sequence is defined in JavaScript?

cso
  • 91
  • 9

3 Answers3

4

\a is not an special sequence (like \n or \t), so the \ falls back to being an escape character, meaning that the character following it will be used literally (even if it were a quote, or a special character).

Hence, '\a' === 'a'.

The second purpose of backslash (the first is printing special character like newline with \n or TAB with \t), is to escape JavaScript special character. For example, to have a string containing a quote, you can either mark the string with double quotes "'" or if you use single quotes, you will need to escape with the backslash, like so: '\'', to prevent the literal ' from terminating the string.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

As you can see in this answer, not every letter has an associated escape sequence. 'a' is one of the letters that does NOT have an escape sequence associated with it, and so to Javascript, there's no special meaning, it's just a backslash and the letter 'a'.

Calvin Godfrey
  • 2,171
  • 1
  • 11
  • 27
-1

only few letters in combination with a backslash form escape sequence (like \n, \f, \r, \b, \t, \v) and \a is not in the list. please refer the following link https://www.w3schools.com/js/js_strings.asp