What is URL encoding?
URL encoding (also called percent encoding) converts characters that are not allowed in URLs into a safe format using a % followed by two hexadecimal digits representing the character's UTF-8 byte value.
For example, a space character becomes %20, and & becomes %26.
encodeURIComponent vs encodeURI
| Function | Use case | Does NOT encode | Encodes |
|---|---|---|---|
| encodeURIComponent | Query param values, path segments | A–Z a–z 0–9 - _ . ! ~ * ' ( ) | : / ? # [ ] @ ! $ & ' ( ) * + , ; = % |
| encodeURI | Full URL encoding | A–Z a–z 0–9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) # | Spaces and non-ASCII only |
| Form encoding | HTML form POST data | A–Z a–z 0–9 - _ . * | Space → +, others → %XX |
Common percent-encoded characters
| Character | Encoded | Common context |
|---|---|---|
| Space | %20 (or + in forms) | Query string values |
| & | %26 | Parameter separator inside values |
| = | %3D | Inside parameter values |
| + | %2B | Literal plus sign in values |
| # | %23 | Fragment identifier inside values |
| / | %2F | Slash inside path segment values |
| ? | %3F | Question mark inside values |
| @ | %40 | Email addresses in URLs |
