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.
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
Related guides
Frequently asked questions
What is percent-encoding in a URL?
What's the difference between encodeURI and encodeURIComponent?
Why do I get double-encoded URLs with %2520 instead of %20?
Related tools
JSON Formatter
Validate, format, and minify JSON. Browse it as a tree, run JSONPath queries, compare two documents, or validate against a JSON Schema.
Use toolUnix Timestamp
Convert between Unix timestamps and human-readable dates.
Use toolCron Generator
Visually build standard 5-part cron expressions or translate them into readable schedules.
Use tool