Percent-encoding replaces each unsafe byte with % followed by two hex digits:
- Encode (URI) —
encodeURI(): encodes the text as UTF-8, then percent-encodes everything except URL structure characters (A–Z a–z 0–9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #). Use on complete URLs. - Encode component —
encodeURIComponent(): encodes a stricter set, including/ ? : @ & = + $ , #. Use on a single value you're placing inside a query string, so its special characters can't break the URL's structure. - Decode —
decodeURIComponent()reverses the process, turning%XXsequences back into characters. Malformed sequences (a%not followed by two hex digits) produce a clear error instead of silent garbage.