TechCompare LogoTechCompare

What is percent-encoding? A guide to URL encoding for developers

Use encodeURIComponent() for individual query parameter values. Use encodeURI() for full URLs. Never hand-roll percent-encoding. If you need URL-safe Base64 (for JWT tokens in URLs), use the URL-safe variant that replaces + with - and / with _.

Percent-encoding (also called URL encoding) replaces characters that aren't safe in a URL with a percent sign followed by two hex digits. Spaces become %20, ampersands become %26, and so on. Any character outside the unreserved set (A-Z, a-z, 0-9, hyphen, underscore, period, tilde) must be percent-encoded for a URL to be valid.

By TechCompare · Updated

Encoding focus
URL encoding guide
url-encoding
Category
Best Practices
Practical encoding guidance

How this is calculated

Different parts of a URL have different reserved characters. The query string reserves ? & = and #. The path reserves / ? and #. This is why you can't just encode everything the same way. JavaScript's encodeURIComponent() encodes everything except the unreserved set, which is correct for query parameter values. encodeURI() is less aggressive and keeps URL structure characters intact, which is correct for encoding a full URL. Using the wrong one causes double-encoding bugs (%%2F instead of %2F) or under-encoding (a raw & in a query value that breaks parsing).

Verdict

The reserved character set changes by URL segment, which is why two helpers exist. encodeURIComponent escapes everything outside the unreserved set, which is right for query values. encodeURI keeps structural delimiters intact, which is right for full URLs. Mixing them up causes double-encoding like %%2F or raw ampersands that break query parsing. For JWTs in URLs, swap + for - and / for _.

More Encoding scenarios

Base64 vs Hex
Base64 and hexadecimal both encode binary data as text, but they serve different purposes.
View details ➜
UTF-8 vs ASCII
ASCII maps 128 English characters to 7-bit values.
View details ➜
Binary-to-text encodings
Binary-to-text encodings convert arbitrary bytes into printable characters so they can travel through text-only channels: JSON, XML, email, URLs, and printed QR codes.
View details ➜

Frequently asked questions

What is percent-encoding in a URL?
Replacing unsafe characters with a percent sign plus two hex digits: a space becomes %20, an ampersand becomes %26. Anything outside the unreserved set (A-Z, a-z, 0-9, hyphen, underscore, period, tilde) must be encoded when it appears as data inside a URL, because otherwise it bends the URL's structure.
What's the difference between encodeURI and encodeURIComponent?
encodeURIComponent escapes everything except the unreserved set, which is right for a single query parameter value where &, =, and ? must not survive. encodeURI preserves structural delimiters like /, ?, and # so a full URL keeps its shape. Using encodeURIComponent on a whole URL breaks it, and using encodeURI on a parameter value lets raw ampersands corrupt the query string.
Why do I get double-encoded URLs with %2520 instead of %20?
Because something encoded the string twice. The first pass turned a space into %20, and the second pass encoded the % sign itself into %25. The fix is to encode exactly once, as late as possible: keep raw values in your application state and call encodeURIComponent at the URL assembly step, not when the value is stored.