JS DOM: escape, unescape 👎

By Xah Lee. Date: . Last updated: .

The Escape Method

escape is a string property of the the Global Object.

escape is deprecated.

escape(str)

Return a new string from str with some characters percent encoded.

console.assert(
 escape("A") === "A",
);

console.assert(
 escape("~") === "%7E",
);

console.assert(
 escape(" ") === "%20",
);

console.assert(
 escape("α") === "%u03B1",
);

/*
α
Name: GREEK SMALL LETTER ALPHA
codepoint: 945
HEXD: 3B1
UTF8: CE B1
UTF16: 3B1
*/

console.assert(
 escape("🦋") === "%uD83E%uDD8B",
);

/*
🦋
Name: BUTTERFLY
Codepoint: 129419
HEXD: 1F98B
UTF8: F0 9F A6 8B
UTF16: D83E DD8B
*/

Forms of the Changed Char

Changed characters are in this form:

, where DD is 2 digits of hexadecimal and DDDD is 4 digits of hexadecimal.

they are the byte sequence of the character in UTF-16 encoding. .

[see JS: String Index and Code Unit]

Chars Not Changed

// all chars that escape() does not change
console.log((Array.from((Array(128)).keys(), (x) => String.fromCharCode(x)).filter((x) => (escape(x) === x))).join(""));
// *+-./0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz

All other chars are replaced.

unescape

Use unescape function to decode a string encoded with escape.