https:example.com/search?q=hello%20world&lang=it&tag=c++%20developer—{"q":"hello world","lang":"it","tag":"c developer"} A URL can only contain a limited set of ASCII characters. Characters outside this set — spaces, accented letters, symbols like & = ? # — must be percent-encoded: replaced with %XX where XX is the hexadecimal UTF-8 byte value. For example, a space becomes %20, and € becomes %E2%82%AC (three UTF-8 bytes).
These are two different functions in JavaScript — choosing the wrong one is a common source of bugs:
| Function | Does NOT encode | Use for |
|---|---|---|
| encodeURI | A–Z a–z 0–9 - _ . ! ~ * ' ( ) ; , / ? : @ & = + $ # | Encoding a complete URL — preserves URL structure |
| encodeURIComponent | A–Z a–z 0–9 - _ . ! ~ * ' ( ) | Encoding a single query parameter value — encodes :/?#&=+ |
Example — encoding a redirect parameter:
A URL has well-defined components, each with its own encoding rules:
%20 into %2520. Always decode before re-encoding.%20 (correct) vs + (only valid in query strings, form-encoded format).All encoding and decoding runs locally in your browser. No data is sent anywhere.