Confused between encodeURI and encodeURIComponent? Learn the exact differences, use cases, and code examples in this developer-friendly guide.
If you’ve worked with URLs in JavaScript, you’ve probably come across two similar-looking functions:
encodeURI()encodeURIComponent()At first glance, they seem almost identical. But using the wrong one can break your URLs, APIs, or query parameters in subtle and frustrating ways.
In this in-depth developer guide, we’ll break down:
You can also test both functions instantly here:
👉 https://www.mydevtoolhub.com/tools/url-encoder-decoder
Many developers misuse these functions, which leads to:
Understanding the difference is not optional—it’s essential.
Encodes a complete URL, but leaves certain characters untouched.
Encodes a single component of a URL, such as a query parameter.
| Feature | encodeURI() | encodeURIComponent() |
|---|---|---|
| Purpose | Encode full URL | Encode URL parts |
| Encodes spaces | ✅ Yes | ✅ Yes |
Encodes & | ❌ No | ✅ Yes |
Encodes = | ❌ No | ✅ Yes |
Encodes / | ❌ No | ✅ Yes |
Encodes ? | ❌ No | ✅ Yes |
| Use case | Full URL | Query params / user input |
const url = "https://example.com/search?q=hello world&category=dev";
const encoded = encodeURI(url);
console.log(encoded);
https://example.com/search?q=hello%20world&category=dev
%20?, &, = are NOT encodedThis is because these characters are essential for URL structure.
const query = "hello world & dev";
const encoded = encodeURIComponent(query);
console.log(encoded);
hello%20world%20%26%20dev
& becomes %26const query = "node js & api";
const url = `https://example.com/search?q=${encodeURI(query)}`;
& is not encodedconst query = "node js & api";
const url = `https://example.com/search?q=${encodeURIComponent(query)}`;
const fullUrl = "https://example.com/search?q=hello world";
const encodedUrl = encodeURI(fullUrl);
Here, the structure remains intact while unsafe characters are encoded.
hello world & test=value
hello%20world%20&%20test=value
hello%20world%20%26%20test%3Dvalue
Use encodeURI() when:
encodeURI("https://example.com/path?q=test value");
Use encodeURIComponent() when:
const name = "John & Sons";
const encodedName = encodeURIComponent(name);
This leads to broken URLs.
This results in over-encoding.
Leads to bugs and security risks.
Creates inconsistent results.
In Node.js, especially with Express:
app.get('/search', (req, res) => {
console.log(req.query.q);
});
If frontend does not encode properly, backend receives incorrect values.
&, =, ?encodeURIComponent for query paramsencodeURI only for full URLsInstead of guessing, use this tool:
👉 https://www.mydevtoolhub.com/tools/url-encoder-decoder
You can:
const inner = "https://example.com?q=hello world";
const encodedInner = encodeURIComponent(inner);
const final = `https://app.com?redirect=${encodedInner}`;
encodeURI is for full URLs, while encodeURIComponent is for parts like query parameters.
Use encodeURIComponent for query parameters.
No, it does not encode important characters like & and =.
Your URL may break or API may misinterpret parameters.
Yes, especially when dealing with dynamic user input.
Understanding the difference between encodeURI and encodeURIComponent is a small but powerful skill that can save you hours of debugging.
As a developer, always remember:
And when you're unsure, test it here:
👉 https://www.mydevtoolhub.com/tools/url-encoder-decoder
Master this concept and build cleaner, bug-free applications.
Learn how to handle special characters, Unicode, emojis, and spaces in URL encoding with real examples and edge-case fixes.
Learn how to debug URL encoding issues in production using logs, network tools, and advanced developer techniques.
Master URL encoding with real-world examples including forms, search queries, APIs, and redirects. A practical guide for developers.