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) 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.

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 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

The reserved character set changes by URL segment, which is why two helpers exist. encodeURIComponent is designed for a component value, but its JavaScript escape set also leaves ! ~ * ' ( ) literal. If strict RFC 3986 unreserved-only output is required, encode those five characters after encodeURIComponent. encodeURI keeps structural delimiters intact for a full URL. Mixing them 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 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?
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.