JavaScript gives you two functions that sound like the same tool with extra letters, and picking the wrong one fails in both directions. Use encodeURI on a single query value and the & inside it survives, quietly splitting your one value into two parameters. Use encodeURIComponent on a whole address and it cheerfully encodes the ://, the ? and every &, leaving you with a string no browser can open. The difference is one question: are you encoding an address, or a value that lives inside one?

This guide walks through that difference, shows the same choice inside the free URL Encoder/Decoder on Ovatly, and works through real outputs so you can see exactly which characters each function touches.

Contents

  • Quick answer
  • One job, two scopes
  • What each function leaves alone
  • The same split in the URL Encoder/Decoder
  • How to choose and apply the right one, step by step
  • Worked example: one value, three results
  • Limitations and honest warnings
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Use encodeURI (Full URL mode in the URL Encoder/Decoder) when you have a complete address: it encodes spaces and stray characters while keeping the address structure intact. Use encodeURIComponent (Component mode) when you have a single value going into a query string, like a search term or a redirect target: it encodes everything a URL cannot carry raw, including &, =, ? and /, so the value stays one value. If the string you are encoding contains a character that means something to a URL, and that meaning must not fire, you need the component version.

One job, two scopes

Both functions do percent-encoding: they replace characters a URL cannot carry raw with a % followed by two hexadecimal digits, so a space becomes %20 and an ampersand becomes %26. Non-ASCII characters are encoded as their UTF-8 bytes, which is why café turns into caf%C3%A9: the accented letter takes two bytes, and each byte becomes its own three-character code.

The job is the same. The scope is what differs. A URL is not a flat string; it has structure. The : and / after the scheme, the ? that opens the query string, the & that separates parameters and the = that binds a name to a value are all load-bearing. Encoding a whole address must preserve that structure or the address stops working. Encoding a single value must do the opposite: any structural character inside the value has to be disarmed, or it will be read as structure instead of content.

What each function leaves alone

encodeURI assumes its input is already a URL. It encodes spaces and stray characters but deliberately leaves the structural characters alone. Run it over https://example.com/search?q=hello world & more and you get:

https://example.com/search?q=hello%20world%20&%20more

The spaces are fixed, but look at the &: it is still raw, so a server reading that query string sees a parameter q set to hello world and then a stray second parameter named more. The function did its job; the input was the wrong scope for it.

encodeURIComponent assumes its input is one component of a URL, a single value. It encodes the structural characters along with everything else. The small input a/b?c=d&e comes out as:

a%2Fb%3Fc%3Dd%26e

Every character that could be mistaken for structure has been neutralised. That string can sit safely as one parameter value, because nothing inside it can open a new parameter or end the value early.

The same split in the URL Encoder/Decoder

The URL Encoder/Decoder makes the identical choice with friendlier names. Component mode encodes everything a URL cannot carry raw, including &, =, ? and /, which is the behaviour of encodeURIComponent. Full URL mode encodes spaces and stray characters while leaving the structural characters alone, which is the behaviour of encodeURI. So if you already know which JavaScript function your situation calls for, you know which mode to pick, and the tool's own guidance matches: one value inside a URL means Component, a whole address with a space in it means Full URL.

One related detail: in query strings produced by form submissions, + means a space. The tool uses %20 instead, which is valid everywhere in a URL, to avoid ambiguity. A + in a query value almost always came from a form; when this tool encodes a space, you will see %20.

How to choose and apply the right one, step by step

  1. Ask what you are holding. A complete address with scheme, host and path means Full URL mode, the encodeURI behaviour. One value destined for a query string means Component mode, the encodeURIComponent behaviour.
  2. Open the URL Encoder/Decoder and choose the mode from step 1.
  3. Paste your text or URL.
  4. Press Encode or Decode.
  5. Copy the result, and sanity-check it: a full address should still show its ? and & characters raw, while an encoded value should show none.

Worked example: one value, three results

Take the search phrase hello world & more, the tool's own example, destined for a query string. In Component mode, encoding gives:

hello%20world%20%26%20more

The ampersand is encoded because in a query string a raw & would start a new parameter. Dropped into https://example.com/search?q= followed by that output, the server receives exactly one parameter with the full phrase as its value.

Now the same phrase through the whole-address scope, shown in the previous section: the & survives and the value splits. And the reverse error, running the component behaviour over the complete address https://example.com/a b?q=x y, gives:

https%3A%2F%2Fexample.com%2Fa%20b%3Fq%3Dx%20y

That looks like a failure, and as an address it is one. But notice what it actually is: a complete URL encoded as a single value. That is precisely the correct treatment for a redirect target, where one URL travels inside another URL's query string as a redirect parameter. The functions are not good and bad versions of each other; each answers a different question, and the boundary is clear: structure raw means address, structure encoded means value.

Limitations and honest warnings

Percent-encoding is a transport fix, not a safety feature. Encoding is not encryption: anyone can decode a percent-encoded string with this same tool, so an encoded password, token or personal detail in a URL is readable by anyone who sees the link. Encoding also does not validate anything. A correctly encoded value can still be a wrong value, and encoding a string twice produces output that needs decoding twice, which is a common source of confusion when links pass through several systems.

The tool also has a strict side, deliberately. Decoding invalid input, like a stray % or an incomplete %2 at the end, produces a clear error instead of garbage, so a damaged string stops at the error rather than decoding partially. Finally, encoded output is longer than the input, since each unsafe character becomes three characters and non-ASCII characters take even more. That growth is normal.

Common mistakes

  • Using the whole-address function on a query value. A raw & or = inside the value survives and splits or corrupts the parameter. Use Component mode for values.
  • Using the component function on a whole address, then sharing it as a link. The encoded :// and ? mean the browser cannot parse it as an address. Use Full URL mode for addresses.
  • Encoding a value that is already encoded. Double encoding turns %20 into %2520, and the value arrives still encoded after one decode. Encode once, at the point where the value enters the URL.
  • Treating + and %20 as interchangeable everywhere. The + convention belongs to form-submitted query strings. In paths and other parts of a URL it is a literal plus sign.
  • Assuming encoded means hidden. Anyone with the same tool can read it. Keep real secrets out of URLs entirely.

FAQs

Which mode do I need? If you are building one value inside a URL, like a search term or a redirect target, use Component. If you have a whole address with a space in it, use Full URL.

What happens if I use encodeURIComponent on a whole URL? It encodes the structure as well as the content, turning https://example.com/a b?q=x y into https%3A%2F%2Fexample.com%2Fa%20b%3Fq%3Dx%20y. The result is a single encoded value, not a working address, which is exactly what you want for a redirect target and exactly wrong for a link.

Why did my encoded text get longer? Each unsafe character becomes three characters, like %20 for a space. Non-ASCII characters take even more, since they are encoded as their UTF-8 bytes.

Is + the same as %20? In query strings produced by form submissions, + means a space. This tool uses %20, which is valid everywhere in a URL, to avoid ambiguity.

Does percent-encoding hide the value I encoded? No. Encoding is not encryption. Anyone can decode a percent-encoded string with this same tool, so never treat an encoded value as hidden or protected.

Conclusion

The choice between encodeURI and encodeURIComponent is a choice of scope: whole address, structure preserved; single value, structure escaped. The URL Encoder/Decoder offers the same two scopes as Full URL and Component mode, so the rule transfers directly. The reverse journey matters just as often, since links that arrive in email and chat apps are usually encoded and hard to read; How to Decode a URL Copied from an Email or Chat App covers that side step by step. For more tools like these, see the Developer Tools guide archive.