Few things cause as many silent bugs as a mishandled URL. A query parameter with a space, a plus sign, or an ampersand in it can quietly break a request without throwing a single error. The fix is almost always URL encoding — and understanding it properly saves you hours of head-scratching.

What URL encoding actually is

A URL can only safely contain a limited set of ASCII characters. Letters, digits, and a handful of symbols like -, ., _ and ~ are fine. Everything else — spaces, accented letters, emoji, and characters that have special meaning in a URL like ?, &, =, and / — needs to be escaped.

URL encoding (also called percent-encoding) replaces those characters with a % followed by their byte value in hexadecimal. A space becomes %20. An ampersand becomes %26. Non-ASCII characters are first encoded as UTF-8 bytes, then each byte is percent-encoded. So é becomes %C3%A9, because its UTF-8 representation is two bytes.

When and why you'd use it

You encode whenever you're putting arbitrary data into a URL where it might collide with the URL's structure. The classic case is query strings.

Say you want to search for the phrase cats & dogs. Drop it in raw and you get:

https://example.com/search?q=cats & dogs

That space breaks the URL, and the & is interpreted as a parameter separator — so the server sees a q of cats and a second, bogus parameter dogs. Encode the value and it behaves:

https://example.com/search?q=cats%20%26%20dogs

Now the whole phrase arrives intact as the value of q.

Other common situations:

  • Building API request URLs from user input or dynamic data.
  • Passing a redirect URL as a query parameter (a URL inside a URL — a reliable source of pain).
  • Encoding path segments that contain slashes or reserved characters.
  • Constructing OAuth signatures and signed URLs, where exact encoding rules matter for the signature to validate.

Encode components, not whole URLs

The single most important habit: encode the pieces, not the finished URL. If you encode an entire URL, you'll clobber the ://, the ?, and the & that give it structure.

In JavaScript this maps neatly onto two functions:

// Encodes a full URL, leaving structural characters intact
encodeURI('https://example.com/a path?q=1')
// 'https://example.com/a%20path?q=1'

// Encodes a single component, escaping ? & = / etc.
encodeURIComponent('cats & dogs')
// 'cats%20%26%20dogs'

Use encodeURIComponent for individual query values and path segments. Use encodeURI only when you have an assembled URL and just want to tidy up stray spaces — which, honestly, you rarely do. Build from components instead.

Even better: let URLSearchParams do it

Modern JavaScript gives you a tool that handles encoding for you, so you never forget:

const params = new URLSearchParams({
  q: 'cats & dogs',
  page: '2'
});
const url = `https://example.com/search?${params}`;
// https://example.com/search?q=cats+%26+dogs&page=2

Notice the space became + here, not %20. That's not a bug — it's the application/x-www-form-urlencoded convention, where + means space in query strings. Both are valid; the server needs to decode them correctly. Which brings us to the gotchas.

Common gotchas

The plus sign problem

In a query string, + often means a space. In a path segment, it does not — it's a literal plus. If a user searches for C++ and you naively build a URL, the + symbols can silently turn into spaces on the server. Encode them as %2B when you mean a literal plus, and be deliberate about whether you're in form-encoded or standard percent-encoding territory.

Double encoding

Encoding an already-encoded string produces garbage. A % becomes %25, so %20 turns into %2520. This happens constantly when a value passes through several layers — a frontend encodes it, a backend proxy encodes it again. The symptom is literal %20 text showing up in your app. Encode exactly once, as late as possible, right before the request goes out.

Reserved vs. unreserved characters

Characters like /, ?, #, [, and ] are reserved — they have meaning in a URL. encodeURIComponent escapes them, which is usually what you want for a value. But encodeURI leaves them alone, since it assumes they're part of the structure. Pick the function that matches your intent.

The fragment and the hash router

Everything after # is the fragment and never gets sent to the server. If you're stuffing data after a hash (common in single-page apps), encode it too, or a stray # or & will confuse your router.

A quick worked example

Imagine building a "share to email" link where the subject and body come from user content:

const subject = 'Check this out: 50% off!';
const body = 'Hey,\nFound this great deal & thought of you.';

const mailto = 'mailto:[email protected]?' +
  'subject=' + encodeURIComponent(subject) +
  '&body=' + encodeURIComponent(body);

// mailto:[email protected]?subject=Check%20this%20out%3A%2050%25%20off!&body=Hey%2C%0AFound...

Without encoding, the % in "50% off", the newline, and the & in the body would each break the link in a different way. With encodeURIComponent around every dynamic value, it just works.

When you're debugging and need to see exactly what a string decodes to — or you want to encode a one-off value without firing up a REPL — a quick URL Encoder / Decoder is handy for checking your work and spotting double-encoding at a glance.

Rules of thumb to keep

  • Encode components, never whole URLs.
  • Prefer URLSearchParams or your language's query-builder over hand-rolling strings.
  • Encode once, and do it as close to the network call as possible.
  • Be explicit about + vs %20 depending on context.
  • When something looks wrong, check for double encoding first.

URL encoding isn't complicated once the mental model clicks: anything that isn't a safe character, or that would collide with the URL's own syntax, gets turned into %XX bytes. Get the component boundaries right and the rest follows.

Photo by Chris Ried on Unsplash