Skip to main content

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:

Comments

Popular Posts

Django static files not working when debug false || debug true

# Django static and media files not working when debug is false In this article you will learn how to fix problem of not loading static files and media in Django even the DEBUG is FALSE. This is the easiest and safest solution. # Problem: Django static and media files not working when debug is false  ➤ Code: settings.py DEBUG = False #True ALLOWED_HOSTS = [ '*' ] #Host name # Problem Fix: Let's see, How you can fix the problem of Django static and media files not working when DEBUB = False : 1.)First way: devserver in insecure mode If you still need to server static locally ( e.g. for testing without debug ) you can run devserver in insecure mode: python manage.py runserver --insecure --insecure: it means you can run serve...

How to remove the date and .html from every blogger post url

#Remove date and .html from blogger post url A Common search term which every blogger search is How to Remove Date From Blogger Post URL or how do I remove date from blogger permalink? Follow the steps below and then date and .html will be removed from the URL of your blogger post. Step 1 : Login to your Blogger blog and select Theme / Template. Step 2 : Click on Edit HTML and paste the below code just above the </head> tag let's see code :   ➤ Code : mycode.js; Copy code <script type='text/javascript' > //<![CDATA[ // BloggerJS v0.3.1 var urlTotal,nextPageToken,postsDatePrefix=!1,accessOnly=!1,useApiV3=!1,apiKey="",blogId="",postsOrPages=["pages","posts"],jsonIndex=1,secondRequest=!0,feedPriority=0,amp="&"[0];function urlVal(){var e=window.location.pathname,t=e.length;return...

Django not able to find static files[Solved]

# Django static files not working In this article, you will learn how to fix the problem of not loading static files. This is the easiest and safest solution. In the new version of Django 3.1 and above the settings.py file uses PATH instead of OS to deal with the directory structure. So all the code, we wrote for our static dirs and things like that will no longer work unless we import os at the top of our settings.py file.. Note : This article related to DEBUG = True , if you have problems with DEBUG = False then you can follow this article; Static files not working when DEGUB= False . Have fun! Let's follow a few steps and, solve the problem of not loading Django static files: Step 1 : Check that "BASE_DIR" is defined in your project settings.py , if not then define it  ➤ Code: settings.py import os BASE_DIR = ...