Ad Space

Input Text

Output

Ad Space

What Is URL Encoding, and When Do You Need It

URLs can only safely contain a limited set of characters — letters, digits, and a handful of punctuation marks like hyphens and periods. Anything outside that set — a space, an ampersand, a slash inside a value, an accented letter, an emoji — has to be converted into a "percent-encoded" form before it can travel safely inside a query string or path segment. Percent-encoding replaces each unsafe byte with a percent sign followed by two hexadecimal digits representing that byte's value, so a space becomes %20 and an ampersand becomes %26. Without this step, a raw space or ampersand typed into a search box or a form field could be misread as the end of one parameter and the start of another, silently breaking the link.

Worked Examples

A space encodes to %20 — so "hello world" becomes "hello%20world". An ampersand encodes to %26, which matters because & is the character that separates one query parameter from the next in a URL like example.com/search?q=value&page=2 — if your actual search text contained an unencoded &, the URL parser would think a second parameter had started. Put together, a search for "cats & dogs" turns into the query value "cats%20%26%20dogs", and the full URL becomes something like example.com/search?q=cats%20%26%20dogs — safe to share, paste, or embed in an email without anything getting cut off or misparsed.

encodeURIComponent vs. encodeURI — a Common Mistake

JavaScript actually has two built-in encoding functions, and mixing them up is one of the most common URL-encoding bugs. encodeURIComponent escapes every character that has special meaning in a URL, including &, =, ?, #, and /, which makes it the correct choice for encoding a single value you're about to insert into a query string. encodeURI, by contrast, leaves those characters alone, because it assumes you're encoding an entire, already-structured URL — one that legitimately needs its slashes and colon (as in https://) to stay intact. This tool always uses encodeURIComponent, since the typical use case is encoding one value, not a whole address. Using encodeURI on a single value by mistake would leave characters like & and = unescaped, defeating the purpose of encoding a query parameter in the first place.

A Brief History of Percent-Encoding

Percent-encoding dates back to Tim Berners-Lee's original design for the URL at CERN around 1990. URLs needed to travel safely through decades-old, text-based internet protocols that already treated certain characters as structurally meaningful — a space could be misread as the end of a line, an ampersand or a slash could be mistaken for part of the URL's own syntax — so data containing one of those characters needed an escape mechanism. The percent sign itself was chosen because it was printable, rarely appeared in identifiers on its own, and made an encoded sequence easy to spot by eye inside otherwise ordinary text.

The scheme was formally standardized in RFC 1738 in 1994, the same document that first defined the URL as a concept for the IETF. It was revised twice more as the web grew: RFC 2396 in 1998 separated general URI syntax from the specifics of any one scheme, and RFC 3986 in 2005 became the current standard, precisely defining which characters are "reserved" for structural use (like the & and = this page's examples above rely on) and which are "unreserved" and never need encoding. JavaScript's encodeURIComponent, the function this tool uses under the hood, is built around that same reserved-versus-unreserved distinction.

Common URL Encoding Mistakes

Encoding an entire URL (including "https://" and the slashes between path segments) with encodeURIComponent is a common misstep — it escapes the colon and slashes too, producing a string that's no longer a working link at all. Double-encoding is another: running an already-encoded string through the encoder a second time turns a literal % into %25, which decodes back to the wrong text. And forgetting to decode at all before displaying user-facing text is a subtler bug — a page that reads a raw query parameter and shows it directly to the user will display "hello%20world" instead of "hello world" unless it's explicitly decoded first.

URL Encoding Terms You Should Know

Percent-Encoding — the formal name for the %XX encoding scheme, where XX is a two-digit hexadecimal byte value; also called URL encoding.

Reserved Characters — characters like &, =, ?, #, and / that have structural meaning inside a URL and must be encoded when they appear as literal data rather than as URL syntax.

Query String — the part of a URL after the ? character, made up of key=value pairs separated by &, where each key and value is typically percent-encoded.

URIError — the error JavaScript throws when decodeURIComponent encounters a malformed percent-encoded sequence, such as a % not followed by two valid hex digits.

Frequently Asked Questions

What is URL encoding used for?

URL encoding (percent-encoding) converts characters that aren't safe inside a URL — spaces, ampersands, slashes, non-ASCII letters — into a %XX hexadecimal form, so they can be safely included in a query string or path segment without breaking the URL's structure.

What's the difference between encodeURIComponent and encodeURI?

encodeURIComponent escapes every reserved character, including &, =, ?, and /, making it correct for encoding a single value that will be placed inside a query string. encodeURI leaves those characters alone because it assumes you're encoding a complete URL where they already have structural meaning. This tool uses encodeURIComponent.

Why does decoding sometimes show an error?

A % character must always be followed by exactly two hexadecimal digits (like %20). If the text has a lone % — not part of a valid percent-encoded sequence — decodeURIComponent throws an error, and this tool shows a clear message instead of failing silently.

Can I use this to encode an entire URL, not just a value?

This tool is built for encoding a single query-string value or path segment, not a full URL — it uses encodeURIComponent, which would also escape the slashes and colon in https://example.com, breaking the URL's structure.

Ad Space