encodeURI vs encodeURIComponent: URL Encoding in JavaScript
Which JavaScript URL encoding function to use, why spaces become %20 or +, and how to fix decoding bugs and %2520 double encoding.

Use encodeURIComponent() for any single piece of a URL you are inserting (a query value, a path segment, a nested URL) and encodeURI() only when you have a complete URL whose structure you trust. That one rule prevents most URL bugs. The rest come from spaces (%20 or +?), from decoding the wrong way, and from encoding something twice. This guide covers all three with outputs I checked in Node.
You can paste any string into the URL Encoder and Decoder to see the result instantly. It uses the same rules as encodeURIComponent().
encodeURI vs encodeURIComponent, side by side
Both functions percent-encode characters as UTF-8 bytes (a space becomes %20, é becomes %C3%A9). The difference is which characters they leave alone:
| Character | encodeURI |
encodeURIComponent |
Why it matters |
|---|---|---|---|
| space | %20 |
%20 |
Same in both |
/ ? # : |
kept | encoded (%2F %3F %23 %3A) |
These define URL structure |
& = + |
kept | encoded (%26 %3D %2B) |
These split and join query parameters |
@ , ; $ |
kept | encoded | Reserved characters |
! * ' ( ) |
kept | kept | Neither function encodes these |
é, ش, emoji |
encoded | encoded | Always UTF-8, percent-encoded |
So encodeURI() is built to leave a URL working, and encodeURIComponent() is built to make a value safe to drop inside a URL.
const url = 'https://example.com/search?q=New York & Co';
encodeURI(url);
// 'https://example.com/search?q=New%20York%20&%20Co'
// The & survives, so the server sees q="New York " and a second, empty parameter " Co".
'https://example.com/search?q=' + encodeURIComponent('New York & Co');
// 'https://example.com/search?q=New%20York%20%26%20Co'
// Correct: q="New York & Co".
That first output is the classic bug: encodeURI() treats the user's & as part of the URL's structure, so it splits the value in two.
Build query strings with URLSearchParams instead
In modern JavaScript I rarely call encodeURIComponent() by hand for query strings. The URL and URLSearchParams APIs encode correctly and handle repeated keys:
const url = new URL('https://example.com/search');
url.searchParams.set('q', 'New York & Co');
url.searchParams.set('city', 'Lahore');
url.toString();
// 'https://example.com/search?q=New+York+%26+Co&city=Lahore'
Notice the +. URLSearchParams follows the HTML form encoding standard (application/x-www-form-urlencoded), where a space is written as +, while encodeURIComponent() writes %20. Both are correct in a query string, and servers decode both. The problems start when you mix the two and decode with the wrong function.
%20 vs +: when a plus means a space
- In a query string or form body,
+means a space. That is the form-encoding standard, and every server framework decodes it that way. - In a URL path,
+is a literal plus sign./files/a+b.pdfis a file calleda+b.pdf, nota b.pdf. decodeURIComponent()does not turn+into a space. It only reverses%XXsequences:
decodeURIComponent('New+York'); // 'New+York' (the + stays)
decodeURIComponent('New%20York'); // 'New York'
new URLSearchParams('q=New+York').get('q'); // 'New York' (form decoding)
So if a value came from a form submission or from URLSearchParams, decode it with URLSearchParams, or replace + with a space before calling decodeURIComponent(). A real-world symptom of getting this wrong: a search for "red shoes" arrives on the server as red+shoes and returns no results.
When you need a literal plus sign in a query value, such as a phone number like +923001234567, it must be encoded as %2B. encodeURIComponent() and URLSearchParams both do that for you; concatenating raw strings does not, and the server receives a space instead of a plus.
Double encoding: the %2520 bug
If you see %2520 in a URL, something encoded a string that was already encoded. %20 became %2520 because the % itself was encoded as %25:
const once = encodeURIComponent('a b'); // 'a%20b'
const twice = encodeURIComponent(once); // 'a%2520b'
This usually happens when one layer encodes a value and passes it to a library (an HTTP client, a router, a redirect helper) that encodes it again. The fix is to decide which layer owns encoding: pass raw values to libraries that build URLs for you, and encode by hand only when you are concatenating strings yourself.
Encoding a URL inside another URL
Redirect parameters and OAuth redirect_uri values contain a whole URL as a value. This is exactly what encodeURIComponent() is for:
const returnTo = 'https://devtoolhouse.com/tools/json-formatter?tab=validate';
'/login?returnTo=' + encodeURIComponent(returnTo);
// '/login?returnTo=https%3A%2F%2Fdevtoolhouse.com%2Ftools%2Fjson-formatter%3Ftab%3Dvalidate'
Without encoding, the inner ? and = would merge with the outer query string, and tab=validate would be read as a parameter of /login.
Path segments: encode each one, never the whole path
To build a path from user input (a username, a file name or a slug), encode each segment separately and then join them with /:
const folder = 'Reports 2026';
const file = 'Q3/Q4 summary.pdf';
`/files/${encodeURIComponent(folder)}/${encodeURIComponent(file)}`;
// '/files/Reports%202026/Q3%2FQ4%20summary.pdf'
The / inside the file name is encoded as %2F, so it stays part of the name instead of creating a fake folder.
The same rules in Python and PHP
| Goal | JavaScript | Python | PHP |
|---|---|---|---|
Encode a value, space as %20 |
encodeURIComponent(v) |
urllib.parse.quote(v, safe="") |
rawurlencode($v) |
Encode a value, space as + |
URLSearchParams |
urllib.parse.quote_plus(v) |
urlencode($v) |
| Build a query string from a dict | new URLSearchParams(obj).toString() |
urllib.parse.urlencode(d) |
http_build_query($arr) |
Decode a form value (+ is a space) |
URLSearchParams |
urllib.parse.unquote_plus(v) |
urldecode($v) |
One Python gotcha: quote() leaves / unencoded by default. Pass safe="" when the value is a single component, otherwise Q3/Q4 stays Q3/Q4 and breaks your path.
Common errors and fixes
| Symptom | Cause | Fix |
|---|---|---|
URIError: URI malformed |
decodeURIComponent() hit a % not followed by two hex digits, such as 100% |
Encode a literal % as %25, or catch the error for untrusted input |
%2520 in the URL |
Encoded twice | Encode once, at the layer that builds the URL |
+ arrives as a space |
A raw + in a query value |
Encode it as %2B with encodeURIComponent() |
Spaces arrive as + |
Decoded with decodeURIComponent() |
Decode form data with URLSearchParams |
A parameter is cut off at & |
Used encodeURI() on a value |
Use encodeURIComponent() for values |
FAQ
What is the difference between encodeURI and encodeURIComponent?
encodeURI() encodes a full URL and leaves characters that give a URL its structure (/ ? # & = : and others) unencoded. encodeURIComponent() encodes those characters too, so it is the right choice for a single value placed inside a URL.
Should a space be %20 or + in a URL?
In a query string both are valid: + comes from form encoding (URLSearchParams, HTML forms) and %20 from encodeURIComponent(). In a URL path, only %20 means a space; + is a literal plus sign.
Why does decodeURIComponent not convert + to a space?
Because + only means a space in form encoding, which decodeURIComponent() does not implement. Use URLSearchParams to decode form-encoded values, or replace + with a space first.
What does %2520 mean in a URL?
That a value was encoded twice. %25 is an encoded %, so %2520 is an encoded %20. Remove one of the two encoding steps.
Which characters are safe in a URL without encoding?
Letters, digits and - _ . ~ are always safe. encodeURIComponent() also leaves ! * ' ( ) unencoded. Everything else should be encoded when it is part of a value.
Try the related tools
Related articles

How to Convert a Unix Timestamp to a Date (JavaScript, Python, SQL)
One-line conversions for JavaScript, Python, MySQL, PostgreSQL, SQLite and MongoDB, plus the seconds, milliseconds and time zone bugs to avoid.
Read article
JWT Expired? How to Debug 401 Unauthorized Errors Step by Step
Decode the token, read the exact jsonwebtoken error, and fix expired tokens, invalid signatures, clock skew and audience mismatches.
Read article
UUID vs Auto-Increment IDs: Which Primary Key Should You Use?
Size, index performance, security and UUID v7 compared, with real examples for PostgreSQL, MySQL and MongoDB, and the hybrid most apps need.
Read article