URL Encoding Explained: How to Safely Encode URLs & Query Parameters (2026)
Published April 2, 2026
Have you ever seen URLs with %20 or %E4%B8%AD in them? That's URL encoding (also called percent encoding). It's essential for safely transmitting data in URLs.
What is URL Encoding?
URLs can only contain a limited set of ASCII characters. When you need to include special characters (spaces, Chinese characters, emojis, etc.) in a URL, they must be percent-encoded.
The format is: % followed by two hexadecimal digits representing the byte value.
Common Encodings
| Character | Encoded | Why |
|---|---|---|
| Space | %20 | Spaces not allowed in URLs |
| 中文 | %E4%B8%AD%E6%96%87 | Non-ASCII characters |
| & | %26 | Reserved for query string separator |
| ? | %3F | Reserved for query start |
| # | %23 | Reserved for fragment |
| / | %2F | Reserved for path separator |
| = | %3D | Reserved for key=value |
When Do You Need URL Encoding?
- Query parameters —
?q=hello+world&lang=中文 - API calls — Passing data via GET requests
- Form submissions — Data from HTML forms is URL-encoded
- Redirect URLs — Encoding destination URLs in parameters
encodeURIComponent() vs encodeURI()
// encodeURI — encodes a full URL (keeps :/?# etc.)
encodeURI("https://example.com/path?q=hello world")
→ "https://example.com/path?q=hello%20world"
// encodeURIComponent — encodes a single parameter value
encodeURIComponent("hello world & more")
→ "hello%20world%20%26%20more"
Rule of thumb: Use encodeURIComponent() for query parameter values, encodeURI() for full URLs.