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

By TechCompare · Updated · How we check

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) represents a byte with a percent sign followed by two hex digits when that byte can't appear literally in the URL component being built. Spaces become %20 and an ampersand inside a query value becomes %26, but an ampersand between query parameters remains a delimiter. The unreserved set (A-Z, a-z, 0-9, hyphen, underscore, period, tilde) is always safe, while reserved characters depend on their component and role.

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 uses ? & = and # as structure, while the path uses / ? and #. JavaScript's encodeURIComponent() leaves A-Z, a-z, 0-9, - _ . ! ~ * ' ( ) unchanged, so it is useful for one query parameter value. For strict RFC 3986 unreserved-only output, also percent-encode ! ' ( ) and *. encodeURI() keeps more URL delimiters intact for a full URL. Using the wrong helper causes double-encoding or lets a raw & in a query value break parsing.

Verdict

Use encodeURIComponent for one component value and encodeURI for a full URL, because the reserved character set changes by URL segment. encodeURIComponent also leaves ! ' ( ) and * literal, so encode those five characters afterward if strict RFC 3986 unreserved-only output is required. Mixing the two helpers up causes double-encoding or raw ampersands that break query parsing.

More Encoding scenarios

Base64 vs Hex
Base64 and hexadecimal both encode binary data as text, but they have 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?
Representing a byte with a percent sign plus two hex digits when it can't safely appear in that URL component. A space becomes %20 and an ampersand in a query value becomes %26, while an ampersand separating query parameters stays a delimiter. Unreserved letters, digits, hyphen, underscore, period, and tilde are always safe.
What's the difference between encodeURI and encodeURIComponent?
encodeURIComponent is for one component value. It leaves letters, digits, - _ . ! ~ * ' ( ) unchanged, so strict RFC 3986 code may encode those five extra characters afterward. 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.