Percent-encoding — what most people call URL encoding — is one of those things you use constantly but rarely stop to understand. Then one day a query string breaks because someone typed an ampersand into a search box, and suddenly you're reading RFC 3986 at 2am.

Let's save you that night.

What URL encoding actually is

A URL can only contain a limited set of ASCII characters. Letters, digits, and a handful of punctuation marks are safe. Everything else — spaces, non-Latin characters, reserved symbols used for structure — has to be represented as one or more bytes, each written as a percent sign followed by two hexadecimal digits.

A space becomes %20. An at sign becomes %40. A character outside ASCII gets encoded as its UTF-8 bytes, so é turns into %C3%A9.

The point is to make a string safe to put inside a URL without it being misinterpreted as structure.

Reserved vs. unreserved characters

The spec splits characters into two groups that matter here:

  • Unreserved characters (A-Z a-z 0-9 - _ . ~) never need encoding.
  • Reserved characters (: / ? # [ ] @ ! $ & ' ( ) * + , ; =) have special meaning in a URL. Whether you encode them depends on where they appear.

A / in the path is a separator. A / inside a query value that should be treated as literal text needs encoding. Same character, different intent.

When you actually need it

You need encoding any time user-supplied or dynamic data lands inside a URL. The usual suspects:

  • Query string values. Search terms, email addresses, filter values — anything with spaces, &, =, or #.
  • Path segments built from data. A product name or username dropped into a route.
  • Redirect URLs passed as parameters. A classic: ?return_to=https://example.com/page?x=1. That inner ? and & will wreck your parsing unless the whole value is encoded.
  • OAuth and signed requests. Many signature schemes require exact, canonical encoding of parameters, or the signature won't match.

The gotchas that bite people

1. Using the wrong function

JavaScript gives you two encoders and they are not interchangeable:

encodeURI('https://ex.com/a b?x=1&y=2');
// 'https://ex.com/a%20b?x=1&y=2'  — leaves & = ? / intact

encodeURIComponent('a b?x=1&y=2');
// 'a%20b%3Fx%3D1%26y%3D2'  — encodes reserved chars too

Rule of thumb: use encodeURI only when you have a whole URL and just want to clean up illegal characters. Use encodeURIComponent for individual pieces — a single query value or path segment. Mixing these up is the most common URL bug I see.

2. Spaces: %20 vs. +

In the path, a space is %20. In the query string of an HTML form submission (application/x-www-form-urlencoded), a space is historically encoded as +. Both show up in the wild. If you decode a form-style query with a plain percent-decoder, your + signs survive as literal plus signs and your spaces vanish. Know which format you're dealing with.

3. Double encoding

Encoding a value that's already encoded gives you nonsense. %20 becomes %2520 (because the % itself gets encoded). This happens when a value passes through two layers that each helpfully encode it. If you're seeing %25 everywhere, you've double-encoded.

4. Forgetting the non-ASCII case

Encoding is byte-based and assumes UTF-8 in modern systems. If some part of your stack uses a different charset, accented characters and emoji come out garbled. Keep everything UTF-8 end to end.

5. Encoding the whole URL including the scheme

Running encodeURIComponent over an entire URL turns https:// into https%3A%2F%2F, which is only correct if that URL is itself a parameter value. If it's your actual request target, you've broken it. Encode the parts, not the structure — unless the URL is a part.

A short worked example

Say you're building a search link where the user searched for coffee & cream and you also want to carry a return URL.

const term = 'coffee & cream';
const returnTo = 'https://shop.example.com/cart?id=42';

const url =
  'https://shop.example.com/search'
  + '?q=' + encodeURIComponent(term)
  + '&return_to=' + encodeURIComponent(returnTo);

console.log(url);
// https://shop.example.com/search?q=coffee%20%26%20cream
//   &return_to=https%3A%2F%2Fshop.example.com%2Fcart%3Fid%3D42

Notice two things. The & in the search term became %26, so it won't be read as a parameter separator. The entire return URL — including its own ? and = — is encoded, so it travels as a single opaque value. On the receiving end:

const params = new URLSearchParams(window.location.search);
params.get('q');          // 'coffee & cream'
params.get('return_to');  // 'https://shop.example.com/cart?id=42'

URLSearchParams handles decoding for you, which is why reaching for the URL and URLSearchParams APIs beats hand-rolling string concatenation most of the time. Build the URL with them and you sidestep a whole category of mistakes.

Debugging encoded strings

When a URL looks wrong, the fastest fix is to decode it and read what's actually there. I keep a quick decoder open for exactly this — paste in the mangled string, see the plain text, spot the double-encoding or the stray +. The URL Encoder / Decoder is handy for this: round-trip a value, confirm it decodes back to what you expect, and compare encoder behaviour without writing a throwaway script.

Quick reference

  • Encode individual values with encodeURIComponent; clean whole URLs with encodeURI.
  • Prefer URL and URLSearchParams for building and parsing.
  • Watch for + vs. %20 in form-encoded data.
  • If you see %25, suspect double encoding.
  • Keep your entire stack on UTF-8.

None of this is hard once the model clicks: encoding exists so that data can live safely inside structure. Figure out which part is structure and which part is data, encode only the data, and the strange bugs mostly disappear.

Photo by Ilya Pavlov on Unsplash