encodeURI vs encodeURIComponent: Why Query Params Break in JS
Use encodeURI() only on full URLs and encodeURIComponent() on query values. See why &, =, and # break redirects, plus URLSearchParams and RFC 3986 edge cases.
Use encodeURI() only when encoding a complete URL whose structure (https://, /, ?, &, =) must stay intact. Use encodeURIComponent() (or URLSearchParams) for every individual query parameter value or dynamic path segment.
Passing a query value through encodeURI() leaves ?, &, =, +, and # unescaped. Your API or browser parser then treats those characters as new query delimiters or a URL fragment, silently truncating your data.
You can compare value, url, and form encoding modes side by side and inspect parsed query parameters in our browser-only URL encoder and decoder.
What actually breaks when you pick the wrong function
Suppose a login flow redirects users back to /search?q=a&b=2#top via a next parameter. Watch what happens when encodeURI() wraps the URL:
const next = "/search?q=a&b=2#top";
// BROKEN: encodeURI() keeps ?, &, =, and # raw
const bad = encodeURI(`https://app.example.com/login?next=${next}`);
// "https://app.example.com/login?next=/search?q=a&b=2#top"
const parsedBad = new URL(bad);
parsedBad.searchParams.get("next"); // "/search?q=a" (truncated!)
parsedBad.searchParams.get("b"); // "2" (leaked into a new param!)
parsedBad.hash; // "#top" (never sent to the server!)
Now encode only the parameter value with encodeURIComponent():
const good = `https://app.example.com/login?next=${encodeURIComponent(next)}`;
// "https://app.example.com/login?next=%2Fsearch%3Fq%3Da%26b%3D2%23top"
new URL(good).searchParams.get("next"); // "/search?q=a&b=2#top"
The reverse mistake breaks just as fast. Calling encodeURIComponent() on a full URL escapes the scheme and path slashes, turning https://example.com/docs/my file.pdf into an unusable string (https%3A%2F%2Fexample.com%2Fdocs%2Fmy%20file.pdf).

Exact characters each function leaves alone
Both functions leave letters (A-Z, a-z), digits (0-9), and - _ . ! ~ * ' ( ) untouched. The only difference is the 11 reserved URL syntax characters:
| Character Group | encodeURI() |
encodeURIComponent() |
Strict RFC 3986 |
|---|---|---|---|
Letters, digits, - _ . ~ |
Kept | Kept | Kept |
Reserved delimiters : / ? # & = + $ , ; @ |
Kept | Encoded (%3F, %26, …) |
Encoded |
Sub-delimiters ! ' ( ) * |
Kept | Kept | Encoded (%21, %27, …) |
Space () |
%20 |
%20 |
%20 |
Percent (%) |
%25 |
%25 |
%25 |
Notice that neither function turns spaces into +. If a backend expects form-style + spaces instead of %20, read our breakdown of URL encoding %20 vs + for spaces.
Prefer URL and URLSearchParams over string concatenation
Building query strings by hand invites missing & separators and double-encoding bugs. In modern browsers and Node.js, URL and URLSearchParams handle escaping automatically:
const url = new URL("https://app.example.com/login");
url.searchParams.set("next", "/search?q=a&b=2#top");
url.searchParams.set("email", "alice+dev@example.com");
url.toString();
// "https://app.example.com/login?next=%2Fsearch%3Fq%3Da%26b%3D2%23top&email=alice%2Bdev%40example.com"
Watch out for double-encoding: never pass an already-encoded value into searchParams.set(). Passing hello%20world encodes the % sign itself into hello%2520world.
Strict RFC 3986 encoding for AWS SigV4 and OAuth
Standard encodeURIComponent() follows the older RFC 2396 spec, so it leaves !, ', (, ), and * unencoded (encodeURIComponent("hello! (v2)*") returns "hello!%20(v2)*").
Strict API signing schemes like AWS Signature Version 4 and OAuth 1.0a require RFC 3986 percent-encoding on those five characters too. Wrap encodeURIComponent() with a one-line regex when signing canonical requests:
function encodeRFC3986(value) {
return encodeURIComponent(value).replace(
/[!'()*]/g,
(ch) => `%${ch.charCodeAt(0).toString(16).toUpperCase()}`
);
}
encodeRFC3986("hello! (v2)*"); // "hello%21%20%28v2%29%2A"
If you are passing Base64 tokens inside a query parameter, remember that standard Base64 contains +, /, and =. Using URL-safe Base64URL avoids extra percent-encoding altogether, as explained in our Base64URL vs Base64 guide.
encodeURI vs encodeURIComponent FAQ
Why does encodeURIComponent() throw URIError: URI malformed?
JavaScript throws URIError when your string contains a lone UTF-16 surrogate (half of a split emoji, often caused by calling .slice() on a string containing multi-byte characters). Fix the truncation with Array.from(str).slice(...) before encoding.
Does decodeURIComponent() turn + into a space?
No. decodeURIComponent("a+b") keeps the literal + sign ("a+b"). Use new URLSearchParams() or replace + with " " first when decoding application/x-www-form-urlencoded form strings.
When should I actually call encodeURI()?
Only when you already have a complete, valid URL from user input or a CMS that might contain spaces or non-ASCII characters in its path, and you never need to escape ?, &, or = inside parameter values.
Test value, url, and form encoding modes or catch %2520 double-encoding bugs instantly in the ToolQuilt URL encoder and decoder.