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.
| Question | escape() | encodeURIComponent() |
|---|---|---|
| Status | Deprecated web legacy | Current JavaScript standard |
| Underlying representation | UTF-16 code units | UTF-8 bytes |
| Non-ASCII form | %XX or %uXXXX | One or more %XX bytes |
| Recommended use | Legacy compatibility only | URI component data |
A visible difference
escape("€")
// %u20AC
encodeURIComponent("€")
// %E2%82%ACThe 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
- If another system explicitly calls
unescape(), reproduce the legacy format and document why. - If you are constructing a URI component today, use
encodeURIComponent(). - If you are constructing a complete URI, evaluate
encodeURI()instead. - Do not use either function as HTML escaping or as a security filter.