How a byte becomes %XX
Percent-encoding works on bytes. RFC 3986 §2.1 defines an escape as % followed by two hex digits holding the byte’s value, so the byte 0x20, a space, is written %20. Text has to become bytes first, and for URLs that means UTF-8. RFC 3986 §2.5 says new URI schemes should encode text as UTF-8 and then percent-encode the bytes, and JavaScript’s encoders and URLSearchParams always use UTF-8.
| Character | UTF-8 bytes | Percent-encoded |
|---|---|---|
| space | 20 | %20 |
é | c3 a9 | %C3%A9 |
€ | e2 82 ac | %E2%82%AC |
😀 | f0 9f 98 80 | %F0%9F%98%80 |
Grüße becomes Gr%C3%BC%C3%9Fe. The ASCII letters stay as they are, and each of the two non-ASCII characters turns into two escapes. A legacy system that uses Latin-1 writes é as the single byte %E9, which a strict UTF-8 decoder rejects. The URL decoder can read both if you pick the charset.
Hex digits are case-insensitive: %2f and %2F are the same byte, and §2.1 recommends uppercase. The percent sign itself has to be written as %25 (§2.4), or a decoder takes it for the start of an escape.
Reserved and unreserved characters
RFC 3986 splits the characters allowed in a URI into two groups. The unreserved ones (§2.3) are A–Z, a–z, 0–9, -, ., _ and ~. They never need escaping, and the RFC asks producers not to escape them.
The reserved set (§2.2) has the general delimiters : / ? # [ ] @, which split a URL into scheme, host, path, query and fragment, and the sub-delimiters ! $ & ' ( ) * + , ; =, which schemes and applications use inside components, like & and = in a query string. A reserved character that is meant as data has to be escaped, or it changes how the URL is parsed.
Anything outside both sets, such as a space, ", <, > or any non-ASCII byte, is not allowed in a URI at all and is always escaped.
Whether a reserved character needs escaping depends on where it sits. The / between path segments must stay a slash, while a / inside the value of one segment must become %2F. That is why you encode each value before you assemble the URL, and never the finished URL.
encodeURIComponent, encodeURI and form encoding
JavaScript has two built-in encoders (ECMA-262 §19.2.6.3 and §19.2.6.4), and URLSearchParams adds a third style. All three keep ASCII letters and digits; they differ in the punctuation they leave alone:
| Function | Punctuation left unescaped | Space |
|---|---|---|
encodeURIComponent | - _ . ! ~ * ' ( ) | %20 |
encodeURI | the same, plus ; / ? : @ & = + $ , # | %20 |
URLSearchParams | - _ . * | + |
The same address through both built-ins:
const u = "https://example.com/a b/é?q=1+1&x=y#top";
encodeURIComponent(u);
// "https%3A%2F%2Fexample.com%2Fa%20b%2F%C3%A9%3Fq%3D1%2B1%26x%3Dy%23top"
encodeURI(u);
// "https://example.com/a%20b/%C3%A9?q=1+1&x=y#top"
encodeURI keeps the structure, so it can repair a URL that contains spaces or non-ASCII text. It cannot protect a value: the + in q=1+1 stays a plus, and new URLSearchParams("q=1+1").get("q") returns "1 1". For every value you insert into a URL, use encodeURIComponent. The URL encoder has all three modes side by side.
+ versus %20
The plus sign for a space does not come from RFC 3986. It belongs to application/x-www-form-urlencoded, the format HTML forms submit, which is now defined in §5 of the WHATWG URL Standard. Its serializer writes spaces as + (§5.2), and its parser turns every + into a space before percent-decoding (§5.1). URLSearchParams implements that standard. decodeURIComponent does not:
new URLSearchParams({ q: "a b+c~" }).toString(); // "q=a+b%2Bc%7E"
new URLSearchParams("q=a+b").get("q"); // "a b"
decodeURIComponent("a+b"); // "a+b"
Python’s urlencode and quote_plus also write + for a space but leave ~ alone: urlencode({"q": "a b+c~"}) returned 'q=a+b%2Bc~'. Both outputs decode to the same value. %20 works in every part of a URL, so prefer it when you build one by hand.
Double encoding
RFC 3986 §2.4 says implementations “must not percent-encode or decode the same string more than once”. Encoding twice turns every % into %25:
encodeURIComponent(encodeURIComponent("a+b")); // "a%252Bb"
The server decodes once and receives the literal text a%2Bb. This usually happens when code encodes a value and then hands it to an HTTP client or framework that encodes query parameters again.
Decoding twice is harder to spot. A Base64 value ab+/8= sent correctly as ab%2B%2F8%3D is ab+/8= after one decode. If another layer then decodes it as form data, the + becomes a space and the value is ab /8=, which no longer decodes to the original bytes. In Python, unquote_plus(unquote("ab%2B%2F8%3D")) returned 'ab /8='.
In both cases, find the layer that owns the encoding and remove the extra one. The URL decoder can decode repeatedly and report how many rounds a string needed, which tells you how many layers there are.
Base64 in URLs: escape it or switch alphabets
Standard Base64 (RFC 4648 §4) uses three characters that are reserved in URLs: + and / in its alphabet, and = for padding. Left unescaped:
+becomes a space in a query string:new URLSearchParams("t=ab+/8=").get("t")returned"ab /8="./splits a path segment, so a router sees two segments where you meant one.=usually survives in a query value, because form parsers split each pair at the first=only.t=TQ==gaveTQ==in bothURLSearchParamsand Python’sparse_qs. Hand-written parsers that split on every=lose the padding.
To avoid all three, either keep standard Base64 and pass it through encodeURIComponent exactly once, or use Base64URL (RFC 4648 §5), which replaces + and / with - and _. Both of those are unreserved, so an unpadded Base64URL string passes through encodeURIComponent unchanged. Padding still becomes %3D; RFC 4648 §5 notes that = is typically percent-encoded in a URI and can be skipped when the length is known, and JWT drops it.
Choose by who defines the format. If you do, use unpadded Base64URL. If the receiver expects standard Base64, for example an API parameter documented that way, percent-encode it and let the receiver decode once. Converting between the two alphabets is covered in Base64 vs Base64URL, and Base64 to Base64URL does it in the browser.
Size of each option
A SHA-256 digest is 32 bytes. This is the digest of hello in five forms:
| Form | Value | Length |
|---|---|---|
| Hex | 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 | 64 |
| Raw bytes, percent-encoded | %2C%F2M%BA_%B0%A3%0E%26%E8%3B%2A%C5%B9%E2%9E%1B%16%1E%5C%1F%A7B%5Es%043b%93%8B%98%24 | 84 |
| Standard Base64 | LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ= | 44 |
| Standard Base64, percent-encoded | LPJNul%2Bwow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ%3D | 48 |
| Base64URL, no padding | LPJNul-wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ | 43 |
For 1 MiB (1,048,576 bytes) in which every byte value occurs equally often:
| Form | Characters | Overhead |
|---|---|---|
| Base64URL, no padding | 1,398,102 | +33.3% |
| Standard Base64 | 1,398,104 | +33.3% |
| Standard Base64, percent-encoded | 1,485,490 | +41.7% |
| Hex | 2,097,152 | +100% |
| Raw bytes, percent-encoded | 2,605,056 | +148.4% |
Percent-encoding raw binary is the most expensive choice, because only the 66 byte values that match unreserved characters stay one character long and the other 190 take three each. Percent-encoded standard Base64 costs about 8 percentage points more than plain Base64: + and / are 2 of its 64 characters, and each one grows to three. General Base64 numbers are in Base64 size overhead.
Encode text as encodeURIComponent, encodeURI or form data, in UTF-8 or a legacy charset.
FAQ
Is URL encoding the same as percent-encoding?
Yes. RFC 3986 calls it percent-encoding, and URL encoding is the everyday name. The application/x-www-form-urlencoded format used by HTML forms is a variant that also writes spaces as +.
Why is a space sometimes %20 and sometimes +?
%20 is the RFC 3986 escape and works in every part of a URL. + means a space only in form-encoded query strings and bodies, which is what URLSearchParams and Python’s quote_plus produce.
Which characters never need percent-encoding?
The unreserved set from RFC 3986 §2.3: A–Z, a–z, 0–9, -, ., _ and ~. Escaping them is allowed, but the RFC says producers should not do it.
Should I percent-encode Base64 or use Base64URL?
If you define the format, use Base64URL without padding: its characters pass through encodeURIComponent unchanged. If the receiver expects standard Base64, keep it and run it through encodeURIComponent exactly once.
How much larger does percent-encoding make data?
Each escaped byte takes three characters. For 1 MiB covering all 256 byte values equally, percent-encoding the raw bytes gave 2,605,056 characters, against 1,398,104 for standard Base64.