encodeURI vs encodeURIComponent: Which One to Use
encodeURI keeps ? & = / # as they are; encodeURIComponent escapes them. Tested examples show which to use for query values, paths and full URLs.
The short answer
Use encodeURIComponent for a piece of a URL, such as a query value, a path segment or a form field. Use encodeURI only for a complete URL that is already correctly structured and just needs spaces and non-ASCII characters escaped. The difference is that encodeURI leaves URL structure characters such as ? & = / # alone, while encodeURIComponent escapes them. The URL Encoder uses encodeURIComponent, so it is the right tool for encoding values.
The difference in one test
Run both functions on the characters that give a URL its structure:
| Function | Input | Output |
|---|---|---|
encodeURI | ;/?:@&=+$,# | ;/?:@&=+$,# (unchanged) |
encodeURIComponent | ;/?:@&=+$,# | %3B%2F%3F%3A%40%26%3D%2B%24%2C%23 |
Both functions leave letters, digits and - _ . ! ~ * ' ( ) unencoded, and both encode spaces as %20 and non-ASCII characters (such as é) as UTF-8 percent sequences. All outputs in this article were checked in Node.js, which uses the same functions as the browser.
Why encodeURI breaks query values
Say a user searches for Tom & Jerry and you build the URL like this:
encodeURI("https://example.com/search?q=" + name)giveshttps://example.com/search?q=Tom%20&%20Jerry
The & was left alone, so the server sees two parameters: q=Tom and a second, nameless one, Jerry. The search term is cut in half.
Encode the value on its own instead:
"https://example.com/search?q=" + encodeURIComponent(name)giveshttps://example.com/search?q=Tom%20%26%20Jerry
Now & became %26 and the server receives the full value Tom & Jerry. The same problem happens with =, # (which starts the fragment and cuts off everything after it), + and ? inside values.
Why encodeURIComponent breaks whole URLs
The opposite mistake is passing a full URL to encodeURIComponent:
- Input:
https://example.com/a b?q=café & co#top encodeURIComponentoutput:https%3A%2F%2Fexample.com%2Fa%20b%3Fq%3Dcaf%C3%A9%20%26%20co%23top
That string is no longer a working URL: the ://, /, ? and # have all been escaped. It is exactly what you want when the whole URL is itself a value, for example a redirect= parameter, but it cannot be used as a link.
With encodeURI, the same input becomes https://example.com/a%20b?q=caf%C3%A9%20&%20co#top. The URL still works, but note that the & in the query was not protected. encodeURI cannot know that this & was meant as data.
A simple rule
| You are encoding | Use |
|---|---|
| A query parameter value | encodeURIComponent |
| A query parameter name | encodeURIComponent |
| One path segment (for example a file name) | encodeURIComponent |
| A full URL passed as a parameter (redirect, share link) | encodeURIComponent |
| A full URL you typed or received, which only has spaces or non-ASCII characters | encodeURI |
In practice, encodeURIComponent is the one you need most of the time. Build URLs from encoded parts rather than encoding the finished string.
Path segments need the same care
The same rule applies to the path. Suppose a file is called Q3 report #2.pdf:
encodeURI("https://example.com/files/" + name)giveshttps://example.com/files/Q3%20report%20#2.pdf. The#is left alone, so the browser treats2.pdfas a fragment and requests a file calledQ3 reportinstead."https://example.com/files/" + encodeURIComponent(name)giveshttps://example.com/files/Q3%20report%20%232.pdf, which requests the right file.
Encode each segment separately. If a value contains a slash that is part of the name rather than a folder separator, encodeURIComponent turns it into %2F; encodeURI would leave it as a real slash and change the path.
Non-English text and emoji
Both functions convert characters outside ASCII to their UTF-8 bytes, each written as % plus two hex digits. So é becomes %C3%A9, the emoji 😀 becomes %F0%9F%98%80 (four bytes), and the Hindi word नमस्ते becomes 18 bytes, or 54 characters of percent-encoding. Browsers usually display the readable form in the address bar, but the encoded form is what is sent to the server. Long non-English values can therefore make a URL much longer than it looks.
Better still: let the URL APIs do it
Modern JavaScript has APIs that encode for you:
new URLSearchParams({ q: "Tom & Jerry" }).toString()givesq=Tom+%26+Jerry.const u = new URL("https://example.com/search"); u.searchParams.set("q", "Tom & Jerry");produces a correctly encoded link inu.href.
Notice that URLSearchParams writes spaces as +, following the HTML form encoding rules, while encodeURIComponent writes %20. Servers that decode query strings treat both as a space. The full story of when + and %20 differ is in %20 vs + in URLs.
Decoding: decodeURI vs decodeURIComponent
The decoders mirror the encoders. decodeURIComponent decodes every percent sequence; decodeURI leaves sequences that stand for structure characters (such as %26 for &) as they are, so that decoding a URL does not change its structure.
Two behaviours to know:
+is not turned into a space.decodeURIComponent("hello+world")returnshello+world. If your data came from an HTML form, replace+with a space before decoding, or useURLSearchParams, which handles it.- A stray
%throws an error.decodeURIComponent("100%")throwsURIError: URI malformed, because%must be followed by two hex digits. Wrap decoding of untrusted input intry...catch.
How to use the URL Encoder
Tom & Jerry or a whole redirect URL.= of your parameter.decodeURIComponent, a lone % is not valid input.Because the encoder uses encodeURIComponent, pasting a full link into it encodes the :// and / too. That is correct if the link is going inside another URL, and wrong if you want a clickable link.
Common mistakes
- Encoding twice.
encodeURIComponent("%20")gives%2520: the%itself is encoded as%25. If you see%25in your URLs, something encoded an already-encoded value. - Using
escape(). It is a deprecated function that does not handle UTF-8 correctly for URLs. UseencodeURIComponent. - Encoding the whole query string with
encodeURIComponent.encodeURIComponent("a=1&b=2")turns the separators into%3Dand%26. Encode each name and value separately, then join them with=and&. - Assuming
encodeURImakes any string safe. It does not encode&,=,?or#, so it is never enough for values.
For a step-by-step guide to building a query string by hand, see how to URL-encode a query parameter. For the basics of percent-encoding, see URL encoding and decoding explained.
Frequently asked questions
What is the difference between encodeURI and encodeURIComponent?
encodeURI leaves URL structure characters such as ; / ? : @ & = + $ , # unencoded, so it suits a complete URL. encodeURIComponent encodes them too, so it suits a single value such as a query parameter.
Which should I use for query string values?
encodeURIComponent. With encodeURI, an & or = inside a value is left as is and splits or corrupts the parameter. For example Tom & Jerry must become Tom%20%26%20Jerry.
Why does decodeURIComponent throw URIError: URI malformed?
The input contains a % that is not followed by two hex digits, such as 100%. Only decode properly percent-encoded text, and wrap decoding of untrusted input in try...catch.
Does encodeURIComponent turn spaces into + or %20?
%20. URLSearchParams and HTML forms use + for spaces in query strings. Servers decoding query strings usually treat both as a space, but decodeURIComponent does not convert + to a space.
Put this guide into practice with our free online tool — no signup required.
Open tool