Technical guide

escape() vs. encodeURIComponent()

Both produce percent signs and hexadecimal digits. They do not encode the same representation and should not be used interchangeably.

Short answer

Use encodeURIComponent() for data placed inside a modern URI component. Use legacy escape() only when reproducing or decoding an old system that explicitly expects its historical format.

Questionescape()encodeURIComponent()
StatusDeprecated web legacyCurrent JavaScript standard
Underlying representationUTF-16 code unitsUTF-8 bytes
Non-ASCII form%XX or %uXXXXOne or more %XX bytes
Recommended useLegacy compatibility onlyURI component data

A visible difference

escape("€")
// %u20AC

encodeURIComponent("€")
// %E2%82%AC

The euro sign is one UTF-16 code unit but three bytes in UTF-8. The outputs therefore express different representations of the same character.

Decision rule

  1. If another system explicitly calls unescape(), reproduce the legacy format and document why.
  2. If you are constructing a URI component today, use encodeURIComponent().
  3. If you are constructing a complete URI, evaluate encodeURI() instead.
  4. Do not use either function as HTML escaping or as a security filter.