Ankaj Gupta
July 15, 2020

Escape sequences in JavaScript | JavaScript escape character

JavaScript escape sequences

Escape sequences in a JavaScript string are used to express code units based on code point numbers and It is a mechanism for quoting a single character, Such as : quotes, backslash and control characters.

The backslash('\') is the escape character, JavaScript was built at a time when unicode was a 16-bit character set, so all character, make a string with just one character in it.

JavaScript has 3-escape types, one which was introduced in ECMAScript 2015 :

  1. Unicode escape sequence.

  2. Hexadecimal escape sequence.

  3. Codepoint escape sequence.

# Type Range Digits Format Example
1 Hexadecimal escape 0x00 to 0xFF 2 \xDD \x1E
2 Unicode escape \251 4 \uDDDD \u006E
3 Codepoint escape 0x00 to 0x10FFFF 1 to 6 \u{D..D} \u{1F639}

Let’s see them escape sequences in more detail :

1.) Unicode escape sequence :

  • The unicode escape sequence is \u<hex>, where \u is a prefix followed by a hexadecimal number <hex> with a fixed length of 4 digits.

  • If you want to escape code points from the entire BMP, then you can use an unicode escape sequence.

  • For example '\u000B1' (Plus OR Minus symbol '±') or '\u0222B' (Integral symbol '')

Let’s see the use of unicode escape sequences:

Example :

  1. <script>
  2. const str = 'I\u0020learn \u0055nicode';

  3. console.log(str); //=>'I learn Unicode'

  4. const reg = /\u0055ni.*/;

  5. console.log(reg.test('Unicode')); //=>true

  6. </script>
Important point of unicode escape sequence :
  • An unicode escape sequence can escape code points in a limited range: from U+0000 to U+FFFF (all BMP code points) because only 4 digits are allowed.

  • The unicode escape sequence most of the time this is enough to represent the commonly used symbols.

2.) Hexadecimal escape sequence :

  • The shortest form is named hexadecimal escape sequence: \x<hex>, where \x is a prefix followed by a hexadecimal number <hex> with a fixed length of 2 digits.

  • For example : '\x40' (symbol '@') or '\x3F' (symbol '?').
Let’s see the use of hexadecimal escape sequences :

Example :

  • The hexadecimal escape sequence in a string literal or regular expression looks this way.

  1. <script>
  2. const str = 'I le\x61rn \x48\x65xadecimal';

  3. console.log(str); //=>'I learn Hexadecimal'

  4. const reg = /\x4A\x61va.*/;

  5. console.log(reg.test('JavaScript')); //=>true

  6. </script>
Important point of hexadecimal escape sequence :
  • A hexadecimal escape sequence can escape code points in a limited range: from U+00 to U+FF because only 2 digits are allowed.

  • But hexadecimal escape is nice for use, Because it’s short.

3.) Codepoint escape sequence :

  • ECMAScript 2015 provides escape sequences that represent code points from the entire Unicode space: U+0000 to U+10FFFF, i.e. BMP and astral planes.

  • The new format is called code point escape sequence: \u{<hex>}, where <hex> is a hexadecimal number with a variable length of 1 to 6 digits.

  • For example : '\u{4B}' (symbol 'K') or '\u{1F620}' (Angry face symbol 😡).

Let’s see the use of Codepoint escape sequence :

Example :

  1. <script>
  2. const str = 'Angry face \u{1F620}';

  3. console.log(str); //=>'Angry face 😡'

  4. const reg = /\u{1F620}/u;

  5. console.log(reg.test('Angry face 😡')); //=>true

  6. </script>
Important point of Codepoint escape sequence :
  • Notice that the regular expression /\u{1F620}/u has a special flag u.

The following characters are reserved in JavaScript strings :

# Escape sequences Result Description
1 \' ' Single quotation
2 \" " Double quotation
3 \\ \ Backslash
Seven other escape sequences are valid in JavaScript :
# Escape sequences Description Unicode
1 \b Backspace U+0008
2 \f Form feed U+000C
3 \n New line U+000A
4 \r Carriage Return U+000D
5 \t Horizontal Tabulator U+0009
6 \v Vertical Tabulator U+000B
7 \0 null character U+0000

Note : Important points of escape sequence;

  • '\0' : null character (U+0000 NULL) (only if the next character is not a decimal digit; else it is an octal escape sequence)

  • Escape sequences strings must be written within quotes,Otherwise JavaScript will misunderstand this string:

JavaScript

Join the discussion

Comments

0 comments

No comments yet — be the first to share your thoughts.

Related Posts