JavaScript escape() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Deprecated function

Definition

escape() is an old function that replaces special characters in text with codes. A space becomes %20, and a letter outside the basic range becomes a code like %u20AC.

It is deprecated. It still works in every browser, but it handles non-English letters in a way that no longer matches how the web works. Learn it so you can read old code, and use encodeURIComponent() in your own.

Remember
escape(string);

string  -> the text to encode
returns -> a new string with unsafe characters as %XX or %uXXXX

NOT encoded: A-Z a-z 0-9 @ * _ + - . /
%XX   -> characters up to code 255 (one byte)
%uXXXX -> every other character

DEPRECATED: use encodeURIComponent() instead.

Syntax

Call escape() by name and give it text. It returns the encoded text and does not change the original.

JavaScript
escape(string);
CallWhat it does
escape("Hello World")The space becomes %20. Result: "Hello%20World".
escape("a&b=c")Result: "a%26b%3Dc".
escape("caf\u00e9")The accented letter becomes a one-byte code. Result: "caf%E9".
escape("\u20ac")The euro sign needs a four-digit code. Result: "%u20AC".

Part of the old JavaScript standard (ES1) and kept in browsers for backward compatibility. It is deprecated: do not use it in new code. See also: unescape(), encodeURIComponent() and encodeURI().

Library declarations
JavaScript
// Global function (ES1, now in Annex B "legacy" features)
escape(string)

// returns a string
// DEPRECATED - use encodeURIComponent() instead

Parameters

ParameterDescription
stringThe text to encode. A value that is not text is converted to text first.

Return Value

Return ValueDescription
A stringA copy of the text where every character except A-Z a-z 0-9 @ * _ + - . / is replaced by %XX (for characters with a code up to 255) or %uXXXX (for all others).

JavaScript Examples

Example 1 — Escape Special Characters

See which characters escape() changes and which it leaves alone.

JavaScript
console.log(escape("Hello World"));
console.log(escape("a&b=c"));
console.log(escape("100%"));
console.log(escape("Mia & Leo!"));
console.log(escape("a+b/c@d*e_f-g.h"));
Try It Yourself

How It Works

  1. Spaces and symbols. A space is %20, & is %26, = is %3D and ! is %21. The % itself is %25.
  2. Some characters stay. Letters, digits and @ * _ + - . / are not changed. That is why the last line is unchanged.
  3. Note the plus and the slash. encodeURIComponent() would encode both of them. escape() does not, which is one reason it is not safe for URLs.

Example 2 — Escape Non-English Letters

This is where escape() is different from everything else on the web. It does not use UTF-8.

JavaScript
console.log("Accent: " + escape("caf\u00e9"));
console.log("Euro: " + escape("\u20ac"));
console.log("Chinese: " + escape("\u4f60"));
console.log("Boundary: " + escape("\u00ff") + " and " + escape("\u0100"));
Try It Yourself

How It Works

  1. One byte. The letter "\u00e9" has the code E9, so it becomes %E9.
  2. %u codes. The euro sign (20AC) and the Chinese character (4F60) do not fit in one byte, so escape() writes %u and four digits.
  3. The boundary. Code FF (255) still fits in one byte and gives %FF. Code 0100 (256) is the first one that needs %u0100.

Example 3 — See Why escape() Is Deprecated

Compare it with encodeURIComponent(), and try to decode the result in the modern way.

JavaScript
const text = "caf\u00e9 \u20ac";

console.log("escape: " + escape(text));
console.log("encodeURIComponent: " + encodeURIComponent(text));

try {
  decodeURIComponent(escape("caf\u00e9"));
} catch (err) {
  console.log("Modern decode of escape(): " + err.name);
}

console.log("Modern round trip: " + (decodeURIComponent(encodeURIComponent(text)) === text));
Try It Yourself

How It Works

  1. Different codes. encodeURIComponent() writes UTF-8 bytes: %C3%A9 for the accented letter and %E2%82%AC for the euro sign. escape() writes %E9 and %u20AC.
  2. Not understood elsewhere. Servers and decodeURIComponent() expect UTF-8. %E9 is not valid UTF-8, so decoding throws a URIError.
  3. The modern pair works. encodeURIComponent() and decodeURIComponent() give the original text back. Use them in new code.

Browser & Runtime Support

escape() is part of the "legacy" features of JavaScript. Every browser and every Node.js version still has it, but it may be removed one day and no new code should use it.

✓ Deprecated · Still works

escape()

Available in all browsers and in Node.js, Deno and Bun, but deprecated.

Legacy Status
Google Chrome All versions · Desktop & Mobile
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Opera All versions
Full support
Samsung Internet All versions · Android
Full support
Android WebView All versions
Full support
Node.js All versions · Server runtime
Supported
Deno All versions · JavaScript runtime
Supported
Bun All versions · JavaScript runtime
Supported
Internet Explorer All versions
Full support
escape() Works, but avoid

Bottom line: It still runs everywhere, so old code keeps working. For new code use encodeURIComponent().

Frequently Asked Questions

It replaces special characters in text with percent codes. Characters up to code 255 become %XX (a space is %20), and all other characters become %uXXXX (the euro sign is %u20AC). Letters, digits and @ * _ + - . / are not changed.
Yes. It still works in every browser and Node.js, but it is listed as a legacy feature and should not be used in new code. It does not encode text as UTF-8, so other software cannot read its result.
Use encodeURIComponent() to encode a value for a URL, and encodeURI() for a whole URL. To turn the text back, use decodeURIComponent() or decodeURI().
encodeURIComponent() writes non-English characters as UTF-8 bytes (%C3%A9) and also encodes + and /. escape() writes codes such as %E9 or %u20AC and leaves + and / alone. Only the encodeURIComponent() result works as a URL value.
No. Escape sequences such as \n (new line) or \" (a quote) are written inside string literals while you write code. The escape() function encodes text at run time into %XX codes. They only share a name.

Next: eval()

Learn how eval() runs JavaScript code written as text, and why it is risky.

eval() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful