Every encoding mistake starts the same way: with a string, a goal, and no questions asked. You have a URL to build or a parameter to inspect, you reach for encodeURIComponent or decodeURIComponent because those are the names you remember, and the result either works by luck or breaks in a way nobody notices for weeks. The functions themselves are well documented — the hard part is deciding which one the string in front of you actually needs. That decision depends on three things you can establish in a few seconds: what kind of string you are holding, which direction the data is flowing, and which layer of the URL it lives in.
This guide turns those three things into an explicit decision framework, then walks through every step on the URL Workbench, where you can watch each decision play out on real strings before you repeat it in code. Nothing you paste into the workbench leaves your browser, so it is a safe place to experiment with strings that contain real customer data or internal endpoints.
The three questions to ask first
Question 1: Am I looking at a whole URL or one value?
A whole URL contains structure: the scheme, the host, the slashes that separate path segments, the question mark that opens the query, the ampersands that join parameters. Those characters are the skeleton — they carry meaning and must survive untouched. A single value is the opposite. Once user text becomes a parameter value, every character in it is data, and any character that could be misread as skeleton has to be smuggled through in percent-encoded form.
https://shop.example.com/search?q=blue shoes&tag=sale
└────────────────────── structure ──────────────────────┘└── values ──┘
Getting this question wrong in the encode direction produces the two classic bugs: encoding a whole URL destroys its structure, and failing to encode a value lets the value's own structure leak into the URL's. Both failure modes are covered in depth in encodeURI vs encodeURIComponent; here we only need the question itself, because the answer is what selects the tool.
Question 2: Am I building or reading?
Direction matters as much as content. If you are building — assembling a URL from parts, placing a value into a query string — you are encoding, and you encode each value just before it enters the structure. If you are reading — you received a URL and need to know what is inside it — you are parsing and decoding, and the order is fixed: parse the URL into its layers first, then decode the individual values afterward. Decoding before parsing is how plus-sign confusion and double-encoding get started, because the decoded text can contain characters that look exactly like structure — a decoded ampersand in the middle of a query string will happily split one parameter into two.
Question 3: Which layer does this string live in?
A URL is layered, and each layer has its own rules about which characters are safe. A space is illegal anywhere in a URL. A literal / is fine inside a path but risky inside a query value. A literal ? inside a query value must be encoded or it opens a new query. A # in a value starts a fragment at the whole-URL layer. The same string can be legal raw at one layer and require encoding at another. Before you encode anything, name the layer out loud: path segment, query key, query value, fragment, or complete URL. The workbench's Boundary switch exists precisely to make this layer visible and undeniable.
The decision framework
| You are holding | Direction | Layer | Action |
|---|---|---|---|
| Text that will become a query value | building | query value | encodeURIComponent each value, then join with structure you write yourself |
| A mostly complete URL with stray illegal characters | building | whole URL | encodeURI — or better, fix the values individually before assembling |
| A full URL you received | reading | whole URL | parse first — protocol, host, path, parameters — decode nothing yet |
| One parameter value from a parsed URL | reading | query value | decodeURIComponent, exactly once, and treat + separately |
A string with % sequences of unknown origin | reading | unknown | decode carefully and inspect the result — it may be double-encoded |
Read the table as a chain of the three questions. The first column answers question 1, the second answers question 2, the third answers question 3, and only when all three are answered does the fourth column become obvious. Most people jump straight to the fourth column — "which function do I call?" — and that is exactly why the answer comes out wrong so often. The workbench walkthrough below exercises each row of the table in order.
Walkthrough: seeing the boundaries in the URL Workbench
Parse first, always
Open the workbench's Parse URL & query tool and paste a URL with a few parameters, including one key that appears twice — search pages send repeated keys constantly, for filters and pagination:
https://shop.example.com/search?q=blue%20shoes&tag=sale&tag=winter&page=2
The tool lays the parts out in order: protocol https, host shop.example.com, path /search, then each parameter exactly as it appears on the wire. Two details deserve attention. First, q is shown as it arrived — blue%20shoes, not blue shoes — because parsing and decoding are separate steps, and collapsing them is where the mistakes start. Second, tag appears twice, sale and winter, and both entries are preserved in order. Most quick-and-dirty parsing collapses repeated keys into one, silently dropping filter or pagination state; the workbench keeps the parameter list faithful to the actual string, so what you see is what the server will receive.
The Boundary switch: same string, two layers
Now take one string and examine it under both boundaries. Switch to the encode pane and enter:
blue shoes & hats / sale?
With the boundary set to Component, every character that could be misread as structure gets escaped:
blue%20shoes%20%26%20hats%20%2F%20sale%3F
The ampersand, the slash, and the question mark are all inert text now — exactly what you want if this string becomes a single parameter value. Switch the boundary to Whole and run the same input again: the structural characters survive raw, and only the spaces become %20. That is the correct output if the string were a complete, mostly well-formed URL. Same input, same tool, two different correct answers — and the switch is what forces you to say out loud which layer you are standing on before you get an output at all.
Decoding: let failures be loud
The decode side of the workbench has a property worth more than any rule of thumb: invalid escapes fail loudly. Enter a string with a stray percent sign that is not part of a valid escape:
progress: 100% done %ZZ
decodeURIComponent throws a URIError on input like this, and the workbench surfaces that error instead of quietly returning mangled text. Loud failure is a feature. Silent tools that replace bad escapes with a literal % or drop them entirely hide the fact that the string was never properly encoded in the first place — which usually means it was encoded twice, or never once, on purpose. If you are staring at a decode error, the string did not come from a single clean encodeURIComponent call, and you should find out where it was produced before deciding what to do with it. That investigation is covered step by step in double URL encoding.
Three worked scenarios
Building a search link with spaces and ampersands
Question 1: the user's search text is one value. Question 2: building. Question 3: query value. So each value gets encoded, and the ampersands that join parameters are structure you type yourself:
const q = "blue shoes & hats";
const tag = "sale";
const url = "https://shop.example.com/search?q=" + encodeURIComponent(q)
+ "&tag=" + encodeURIComponent(tag);
// https://shop.example.com/search?q=blue%20shoes%20%26%20hats&tag=sale
Look at the result closely. The & before tag= is raw because it is skeleton; the & inside the search text became %26 because it is data. Two identical characters, two different treatments, decided entirely by the layer each one lives in. The unencoded version of the same concatenation would split q into a truncated value and a bogus hats parameter — the failure mode behind countless broken filter links.
Inspecting a mysterious tracking URL
You received this from an analytics report and something looks off:
https://checkout.example.com/thanks?utm_campaign=fall%2Bsale&rid=A%2Fb9%3D
Reading direction, whole URL — so parse first. The workbench shows two parameters, exactly as they arrived. Now decode value by value: utm_campaign is fall%2Bsale on the wire, which decodes to the literal string fall+sale. Whether that plus should be read as a plus or as a space depends on which system produced it, and the distinction is easy to get wrong — plus signs and spaces explains the history. rid decodes to A/b9=, containing a slash and an equals sign; had those been left raw in the query, the URL might still work, but a strict parser on the receiving end could reasonably disagree. Parse the whole, then decode per value, and the structure of what you received is never in doubt.
Decoding a value that contains %2B
One last trap. A parameter arrives as tag=%2Bsale. You decode it once and get +sale. Now the question: was the original value meant to be the literal string +sale, or sale with a leading space that some layer turned into a plus? If the producer encoded a literal plus with encodeURIComponent, the wire form would be %2B — so decoding once and reading a literal plus is correct, and converting the plus to a space would corrupt the value. But if the producer wrote the value with a raw space and let a form encoder substitute +, the space reading is right. The wire form is your only evidence: %2B says someone deliberately encoded a plus, while a bare + says this may be a space in disguise. That ambiguity is exactly why decoding should happen once, deliberately, at the value layer — and never again after that.
The rule
Everything above compresses into one sentence: encode once, at the boundary, on purpose. Once — a value that gets encoded twice is a different string at every layer it passes through. At the boundary — the moment a value enters a URL's structure, no earlier and no later. On purpose — because you answered the three questions: whole URL or one value, building or reading, which layer. The workbench makes each answer visible before you commit it to code; the framework makes sure you ask the questions at all.
Reproduce every example locally with the JSON Inspector or the matching tool on the workbench. Nothing you paste leaves your browser.