URL Encoding Explained: What %20 and %3A Really Mean
Every developer has seen %20 in a URL and wondered what it means. This guide covers percent-encoding, RFC 3986, the difference between encodeURI and encodeURIComponent, and when to use each.
What %20 Actually Is
A URL is a string with structure: ? starts the query, & separates parameters, = splits a key from its value, # starts the fragment and / separates path segments. If one of those characters appears inside your data — a search term containing “&”, a file name with a space — the parser cannot tell data from structure.
Percent-encoding, defined in RFC 3986, solves this by replacing a byte with % followed by two hex digits. A space is byte 0x20, so it becomes %20; a colon is 0x3A, so %3A. Characters outside ASCII are first converted to UTF-8 and every byte is encoded separately, which is why one visible character can turn into two, three or four escapes:
| Input | UTF-8 bytes | Encoded |
|---|---|---|
| space | 20 | %20 |
| é | C3 A9 | %C3%A9 |
| € | E2 82 AC | %E2%82%AC |
| 😀 | F0 9F 98 80 | %F0%9F%98%80 |
Hex digits are case-insensitive (%3a and %3A mean the same byte), but RFC 3986 recommends uppercase, and that is what JavaScript produces.
Unreserved, Reserved and Everything Else
RFC 3986 sorts characters into three groups, and the group decides whether you must encode:
| Group | Characters | Rule |
|---|---|---|
| Unreserved | A–Z a–z 0–9 - . _ ~ | Never needs encoding. Encoding them is allowed but pointless. |
| Reserved (gen-delims) | : / ? # [ ] @ | Have structural meaning. Encode them when they are part of a value. |
| Reserved (sub-delims) | ! $ & ' ( ) * + , ; = | Meaning depends on the component and the server. Encode inside values to be safe. |
| Everything else | space, " < > \ ^ ` { | } %, non-ASCII | Must always be encoded. |
Note that % itself is in the last group. A literal percent sign in a value has to become %25, otherwise “50% off” will be read as an escape sequence % o and fail.
encodeURI vs encodeURIComponent
JavaScript ships two functions with different jobs. encodeURI assumes you hand it a whole URL, so it leaves every character that has structural meaning alone. encodeURIComponent assumes you hand it a single value, so it encodes the structural characters too.
encodeURI("https://example.com/search?q=hello world&lang=en")
// "https://example.com/search?q=hello%20world&lang=en"
const query = "price >= 100 & category=books";
"https://example.com/search?q=" + encodeURIComponent(query)
// "https://example.com/search?q=price%20%3E%3D%20100%20%26%20category%3Dbooks"If you had used encodeURI on the second value, the & and = would have stayed literal and the server would see two parameters: q=price >= 100 and category=books. No error, just wrong data — the most common URL-encoding bug.
| Character | encodeURI | encodeURIComponent | URLSearchParams |
|---|---|---|---|
| space | %20 | %20 | + |
| & = ? / # : | kept | %26 %3D %3F %2F %23 %3A | encoded |
| + | kept | %2B | %2B |
| ! ' ( ) * | kept | kept | %21 %27 %28 %29, * kept |
| ~ | kept | kept | %7E |
| é | %C3%A9 | %C3%A9 | %C3%A9 |
The row people miss is ! ' ( ) *: encodeURIComponent does not encode them, because they were considered “mark” characters in the older RFC 2396. That is usually harmless, but some OAuth 1.0 signatures and S3-style signing schemes require strict RFC 3986 encoding. MDN suggests this wrapper:
const encodeRFC3986 = (s) =>
encodeURIComponent(s).replace(/[!'()*]/g,
(c) => "%" + c.charCodeAt(0).toString(16).toUpperCase());
encodeRFC3986("it's (fine)*!")
// "it%27s%20%28fine%29%2A%21"The + vs %20 Problem
HTML forms submitted with GET or with Content-Type: application/x-www-form-urlencoded use a slightly different scheme from RFC 3986: a space becomes +, and a real plus sign must be sent as %2B. URLSearchParams and url.searchParams implement that form encoding, in both directions:
new URLSearchParams({ q: "rock & roll", tag: "a+b" }).toString()
// "q=rock+%26+roll&tag=a%2Bb"
new URLSearchParams("q=a+b").get("q") // "a b" (+ decoded as space)
decodeURIComponent("a+b") // "a+b" (+ left alone)The asymmetry causes a classic bug. Someone builds ?email=ana+test@example.com by string concatenation, the server parses the query as form data, and the account lookup searches for ana test@example.com. Encode the value and the plus survives:
"email=" + encodeURIComponent("ana+test@example.com")
// "email=ana%2Btest%40example.com"
new URLSearchParams("email=ana%2Btest%40example.com").get("email")
// "ana+test@example.com"Rule: decode with the same scheme you encoded with. If the query was produced by a form or by URLSearchParams, parse it with URLSearchParams (or your framework's query parser), not with decodeURIComponent.
Which Function for Which Part of the URL
| You are building… | Use | Why |
|---|---|---|
| A query string from key/value pairs | new URL() + url.searchParams.set() | Handles &, =, + and Unicode; no manual joining. |
| One path segment (a slug, a file name) | encodeURIComponent | Encodes / so the value stays one segment. |
| A full URL typed by a user, with spaces | new URL(input) | Parses and normalises; throws on garbage instead of guessing. |
| A redirect target inside another URL | encodeURIComponent on the whole inner URL | Its ?, & and # must not leak into the outer URL. |
| A request body for a classic form POST | URLSearchParams as the body | fetch sets the right Content-Type for you. |
const url = new URL("https://example.com/search");
url.searchParams.set("q", "50% off & free");
url.href
// "https://example.com/search?q=50%25+off+%26+free"
new URL("https://example.com/a b/ñ?q=x y").href
// "https://example.com/a%20b/%C3%B1?q=x%20y"Notice that the URL parser writes the space in the query as %20 when it normalises raw input, but searchParams.set writes +. Both are decoded correctly by servers that treat the query as form data; just don't mix manual string building with either.
Common Mistakes and How to Spot Them
Double encoding: %2520 in your logs
If you see %2520, something encoded an already-encoded string: the % of %20 became %25.
encodeURIComponent(encodeURIComponent("a b")) // "a%2520b"It usually happens when a helper encodes values and a caller also encodes them, or when a value read from location.search (still encoded) is passed to searchParams.set. Encode exactly once, at the point where the URL is built.
URIError: URI malformed
decodeURIComponent throws when a % is not followed by two hex digits ("100%") or when the bytes are not valid UTF-8. The second case shows up with legacy systems that encoded in Latin-1: the deprecated escape("café") returns caf%E9, and decoding %E9 as UTF-8 fails. Wrap decoding of untrusted input in try/catch and return a 400 rather than a 500.
The encoder can throw too: encodeURIComponent("\uD800") raises the same URIError, because a lone surrogate cannot be converted to UTF-8. This happens when a string is cut in the middle of an emoji with slice().
Encoded slashes in the path
encodeURIComponent("docs/2026") gives docs%2F2026, which is correct for a single segment. Some servers and proxies refuse it anyway: Apache, for example, answers 404 for %2F in a path unless AllowEncodedSlashes is enabled. If a value can contain slashes, prefer putting it in the query string.
Encoding the whole URL with encodeURIComponent
encodeURIComponent("https://example.com/a") returns https%3A%2F%2Fexample.com%2Fa, which is no longer a navigable URL. That form is only right when the URL is itself a parameter value, such as /login?next=https%3A%2F%2Fexample.com%2Fa. When you read such a parameter back, validate it against an allow-list of hosts before redirecting to it — otherwise you have an open redirect.
A Quick Debugging Routine
- Copy the exact URL the browser or client sent (DevTools → Network → the request → Headers).
- Decode the suspicious part once with the URL encoder/decoder. If the result still contains
%XXsequences, it was double-encoded. - Check for
+: is the receiver treating the query as form data? Then a literal plus needed%2B. - Check non-ASCII:
%E9(one byte) means Latin-1;%C3%A9means UTF-8. - Rebuild the URL with
new URL()andsearchParamsinstead of string concatenation, and compare.
Base64 strings inside URLs are a related trap, because standard Base64 contains +, / and =. The Base64 guide covers the URL-safe variant used by JWTs.
Encode and decode URLs in your browser
Paste a URL or a value to percent-encode or decode it, with both component encoding and form encoding (+ for spaces). Nothing is sent to a server.
Open URL Encoder →