JS: escape

By Xah Lee. Date: . Last updated: .

escape is a global function object.

[see JS: the Global Object]

escape is deprecated.

escape(str)

Return a new string from str, with some character replaced by one of the following form

, where DD is 2 digits of hexadecimal and DDDD is 4 digits of hexadecimal. (note: a hexadecimal digit is 4 bits. So, 2 hexadecimal digits is 1 byte.)

The hexadecimal are the code unit of the character. (that is, the first 2 bytes of the character in UTF16)

[see JS: String Code Unit vs Code Point]

The chars that does not change are:

All other chars are replaced.

Example, not changed characters.

// not changed
console.log ( escape ("a") === "a" );
// true

// not changed
console.log ( escape (".") === "." );
// true

Example, changed characters.

// changed
console.log ( escape (" ") === "%20" );
// true

// changed
console.log ( escape (",") === "%2C" );
// true

NON-ASCII characters are all changed.

console.log ( escape ( "α" ) === "%u03B1" );
// true

// α
// Name: GREEK SMALL LETTER ALPHA
// codepoint 945
// codepoint hexadecimal: 3b1

Example of Unicode character with codepoint ≥ 2^16.

console.log ( escape ( "😂" ) === "%uD83D%uDE02" );
// true

// same as
console.log (
 "%u" +
 // first byte
 "😂". charCodeAt (0) .toString (16) . toUpperCase() +
 "%u" +
 // second byte
 "😂". charCodeAt (1) .toString (16) . toUpperCase()
);

// 😂
// name: FACE WITH TEARS OF JOY
// codepoint decimal: 128514
// codepoint hexadecimal: 1f602
// UTF 16 encoding: D8 3D DE 02

Check Which Character Are Changed

// print out all ASCII chars that escape() does not change

for (let i = 0; i < 128; i++) {
    let ch = String.fromCharCode(i);
    if ( escape(ch) === ch ) {
        console.log(  i + " " + ch);
    }
}

// 42 *
// 43 +
// 45 -
// 46 .
// 47 /
// 48 0
// 49 1
// 50 2
// 51 3
// 52 4
// 53 5
// 54 6
// 55 7
// 56 8
// 57 9
// 64 @
// 65 A
// 66 B
// 67 C
// 68 D
// 69 E
// 70 F
// 71 G
// 72 H
// 73 I
// 74 J
// 75 K
// 76 L
// 77 M
// 78 N
// 79 O
// 80 P
// 81 Q
// 82 R
// 83 S
// 84 T
// 85 U
// 86 V
// 87 W
// 88 X
// 89 Y
// 90 Z
// 95 _
// 97 a
// 98 b
// 99 c
// 100 d
// 101 e
// 102 f
// 103 g
// 104 h
// 105 i
// 106 j
// 107 k
// 108 l
// 109 m
// 110 n
// 111 o
// 112 p
// 113 q
// 114 r
// 115 s
// 116 t
// 117 u
// 118 v
// 119 w
// 120 x
// 121 y
// 122 z

unescape()

Use unescape(…) function to decode a string encoded with escape(…).

Reference

ECMAScript® 2016 Language Specification#sec-additional-properties-of-the-global-object

Character Topic

  1. JS: String Code Unit vs Code Point
  2. JS: Convert Character To/From Codepoint
  3. JS: String.fromCodePoint
  4. JS: String.fromCharCode
  5. JS: String.prototype.charAt
  6. JS: String.prototype.charCodeAt
  7. JS: String.prototype.codePointAt
  8. JS: Convert Decimal/Hexadecimal
  9. JS: Unicode Character Escape Sequence

URL Topic

  1. JS: encodeURI
  2. JS: encodeURIComponent
  3. JS: escape
  4. JS: Get URL (window.location)

String Topic

  1. JS: Source Code Charset/Encoding
  2. JS: String Code Unit vs Code Point
  3. JS: Unicode Character Escape Sequence
  4. JS: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML/XML Entity List

  1. JS: Template String
  2. JS: Convert String to Number
  3. JS: Encode URL, Escape String
  4. JS: Format Number
  5. JS: JSON Object

  1. JS: String Object
  2. JS: String.prototype
  3. JS: String Constructor
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon