What is URL Encoding?
URL encoding converts characters into a format that can be transmitted over the Internet. URLs can only be sent over the Internet using the ASCII character-set.
The Problem with Special Characters
Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format. URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.
Common URL Encodings
| Character | From ASCII | URL Encoding |
|---|---|---|
| (space) | 32 | %20 or + |
| ! | 33 | %21 |
| # | 35 | %23 |
| $ | 36 | %24 |
| & | 38 | %26 |
| / | 47 | %2F |
| : | 58 | %3A |
Encoding Functions in JavaScript
If you are a developer, you might use these functions to encode URLs:
encodeURI(): Encodes a complete URI.encodeURIComponent(): Encodes a URI component (like a query parameter).
const url = "https://example.com/search?q=hello world"; const encoded = encodeURI(url); // Results: https://example.com/search?q=hello%20world