JavaScript has two built-in functions for percent-encoding, and the names don't make the difference obvious. Picking the wrong one is a common source of broken links, truncated query values and parameters that mysteriously change.
The short answer
encodeURIComponent: for one piece of a URL, such as a query value or a path segment. It escapes every character with special meaning in URLs.encodeURI: for a complete URL that's already structured correctly but may contain spaces or non-ASCII characters. It leaves the characters that form the URL's structure alone.
In practice, you'll use encodeURIComponent (or URLSearchParams) far more often.
What each escapes
Both leave letters, digits and - _ . ! ~ * ' ( ) unchanged and percent-encode spaces, non-ASCII characters (as UTF-8 bytes) and most punctuation. The difference is the reserved characters:
| Character | encodeURI |
encodeURIComponent |
|---|---|---|
; , / ? : @ & = + $ |
Kept | Escaped |
# |
Kept | Escaped |
| Space | %20 |
%20 |
é |
%C3%A9 |
%C3%A9 |
const url = "https://example.com/search?q=rock & roll&lang=en";
encodeURI(url);
// 'https://example.com/search?q=rock%20&%20roll&lang=en'
// the & inside the value is kept, so the server sees q="rock " and a stray " roll" parameter
encodeURIComponent("rock & roll");
// 'rock%20%26%20roll'
The classic bug
Building a query string by concatenation with encodeURI, or with no encoding at all:
const q = "C++ & Java";
fetch("/api/search?q=" + q); // server reads q as "C" plus spaces: + means space and & ends the value
fetch(encodeURI("/api/search?q=" + q)); // still broken: encodeURI keeps & and +
fetch("/api/search?q=" + encodeURIComponent(q)); // correct: q=C%2B%2B%20%26%20Java
Let URL and URLSearchParams do it
The URL API encodes each part correctly and avoids manual concatenation:
const url = new URL("https://example.com/search");
url.searchParams.set("q", "C++ & Java");
url.searchParams.set("page", "2");
url.toString(); // 'https://example.com/search?q=C%2B%2B+%26+Java&page=2'
Note that URLSearchParams encodes spaces as +, following the form encoding rules. Servers decode that correctly in the query string. If you need %20 (some signature schemes require it), use encodeURIComponent for each value instead.
Reading values back decodes them for you:
new URL("https://example.com/?q=C%2B%2B+%26+Java").searchParams.get("q"); // 'C++ & Java'
Path segments
For a value that becomes part of a path, encodeURIComponent is right because it escapes /:
const file = "reports/2026 Q3.pdf";
`/files/${encodeURIComponent(file)}`; // '/files/reports%2F2026%20Q3.pdf'
Without escaping, the slash would create an extra path level. Some servers decode %2F before routing, so test this with your framework if file names may contain slashes.
When encodeURI is the right tool
encodeURI fits a narrow case: you have a full URL, for example from a config file or user input, with literal spaces or non-ASCII characters, and its structure is correct:
encodeURI("https://example.com/files/résumé final.pdf");
// 'https://example.com/files/r%C3%A9sum%C3%A9%20final.pdf'
Don't call it on a URL that's already encoded: %20 becomes %2520. new URL(input).href is often a better choice, since it normalizes without double-encoding existing escapes.
Decoding
decodeURIComponent("rock%20%26%20roll"); // 'rock & roll'
decodeURIComponent("C%2B%2B+Java"); // 'C+++Java' (+ isn't decoded)
decodeURIComponent doesn't turn + into a space. For form-encoded data, replace + first, or parse it with URLSearchParams:
new URLSearchParams("q=C%2B%2B+Java").get("q"); // 'C++ Java'
Wrap decoding of untrusted input in try/catch: a lone % throws a URIError.
In other languages
| Language | Component encoding | Notes |
|---|---|---|
| Python | urllib.parse.quote(value, safe="") |
quote_plus uses + for spaces; urlencode builds query strings |
| PHP | rawurlencode() |
urlencode() uses + for spaces |
| Go | url.PathEscape, url.QueryEscape |
QueryEscape uses + for spaces |
| Java | URLEncoder.encode(value, UTF_8) |
Form encoding: spaces become + |
| Shell | curl --data-urlencode "q=C++ & Java" -G URL |
curl encodes the value for you |
Percent-encoding makes a value safe inside a URL; it doesn't make it safe inside HTML. For that you need HTML escaping. And if you need to put binary data in a URL, Base64URL is usually more compact.