encodeURIComponent()
Encodes a URI component by escaping special characters.
Syntax
encodeURIComponent(str)
Returns
string — The encoded string.
Throws
URIError— The input contains a lone surrogate.
Examples
console.log(encodeURIComponent("a b&c=d"));
Output
a%20b%26c%3Dd
const q = encodeURIComponent("café/parís");
console.log(q);
Output
caf%C3%A9%2Fpar%C3%ADs
Notes
- Escapes everything except `A-Z a-z 0-9 - _ . ! ~ * ' ( )`.
- Use this for query string values; use `encodeURI()` for a full URL.
- Reverse with `decodeURIComponent()`.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |