URL Encoding Explained — encodeURI vs encodeURIComponent
URLs can only contain a limited set of ASCII characters. Percent-encoding (URL encoding) converts any other character into a safe %XX format. Understanding when to use encodeURI vs encodeURIComponent prevents subtle bugs in API calls, redirects, and query strings.
What URL Encoding Is
RFC 3986 defines which characters are allowed in a URL. Characters outside the allowed set must be percent-encoded: replaced with a % followed by two uppercase hex digits representing the character's UTF-8 byte value.
Which Characters Get Encoded
| Category | Characters | Encode? |
|---|---|---|
| Unreserved | A-Z a-z 0-9 - _ . ~ | Never |
| Reserved (structure) | : / ? # [ ] @ ! $ & ' ( ) * + , ; = | Only when used as data |
| Other ASCII | Spaces, <, >, ", {, }, |, \, ^, ` | Always |
| Non-ASCII | é, 中, emoji, etc. | Always (UTF-8 bytes) |
encodeURI vs encodeURIComponent
JavaScript has two built-in encoding functions with important differences:
| Function | Does NOT encode | Use for |
|---|---|---|
| encodeURI() | A-Z a-z 0-9 - _ . ~ ; , / ? : @ & = + $ # | A complete URL |
| encodeURIComponent() | A-Z a-z 0-9 - _ . ~ | A single query param value or path segment |
Query String Encoding Pitfalls
The safest way to build query strings in JavaScript is with the URLSearchParams API:
Python, Go, and curl Equivalents
Related Tools
Related Guides
FAQ
What is URL encoding?
URL encoding (also called percent-encoding) converts characters that are not allowed in URLs into a safe format. Each unsafe character is replaced with a % followed by two hex digits representing the character's UTF-8 byte value. For example, a space becomes %20, and & becomes %26.
When should I use encodeURIComponent vs encodeURI?
Use encodeURIComponent for individual query parameter values and URL fragments. Use encodeURI for a complete URL where you want to preserve the structure (slashes, colons, question marks). If you're building a URL from dynamic values, encodeURIComponent is almost always what you want.
Why does a space become + in some URLs?
In the application/x-www-form-urlencoded format (used by HTML forms), spaces are encoded as + rather than %20. This is the older encoding. In modern percent-encoding (RFC 3986), spaces are always %20. Some servers accept both, but %20 is more portable.
How do I encode a full URL with query parameters in JavaScript?
Use the URL and URLSearchParams APIs: const url = new URL('https://example.com/search'); url.searchParams.set('q', 'hello world'); url.toString() → 'https://example.com/search?q=hello+world'. This handles encoding automatically.
What characters are safe in a URL without encoding?
Unreserved characters are always safe: letters (A-Z, a-z), digits (0-9), and the four characters - _ . ~. These never need encoding. Reserved characters (like /, ?, #, &, =) are safe only in their structural position; if they appear as data values, they must be percent-encoded.