Back to all guides
Developer Tools8 min read

How URL Percent-Encoding Works: RFC 3986, Reserved Characters, and encodeURI vs encodeURIComponent

Master URI syntax rules, query string serialization, handling spaces (%20 vs +), and preventing URL injection vulnerabilities across web APIs.

A
Aakash Sharma
Creator of Softnag & Full-Stack Developer
Published: August 20, 2026Updated: August 24, 2026
How URL Percent-Encoding Works: RFC 3986, Reserved Characters, and encodeURI vs encodeURIComponent - Developer Tools Illustrated Guide
Developer Tools

Developer Tools technical reference asset

Share this guide

Uniform Resource Identifiers (URIs) and URLs are the addressing system of the World Wide Web. However, because URLs must travel across diverse legacy networking hardware, routers, and proxies, the URI specification strictly limits permitted characters to a small subset of US-ASCII.

Percent-encoding (often called URL encoding) represents arbitrary characters—including spaces, symbols, and non-Latin Unicode text—using safe, universally parsable triplets of a percent sign `%` followed by two hexadecimal digits.

Why URLs Require Percent-Encoding: The ASCII US-ASCII Constraint#

The internet protocol suite was built when 7-bit ASCII was standard. Characters outside the US-ASCII printable range (such as emojis or accented letters like `é` or `ñ`), as well as control characters (tabs, newlines), cannot be transmitted directly in raw URL strings.

Furthermore, certain characters (like `?`, `&`, `/`, and `#`) carry reserved structural meanings in URI grammar. If a user searches for `"salt&pepper"`, the `&` must be encoded so the server does not misinterpret it as a query parameter separator.

Reserved vs Unreserved Characters (RFC 3986)#

RFC 3986 divides the ASCII character set into two distinct categories:

CategoryCharacter SetEncoding Requirement
Unreserved Characters`A-Z`, `a-z`, `0-9`, `-`, `_`, `.`, `~`Never encoded. Safe anywhere in a URI.
Reserved (Gen-Delims)`: / ? # [ ] @`Must be encoded when used as raw data payload.
Reserved (Sub-Delims)`! $ & ' ( ) * + , ; =`Must be encoded when used as raw data payload.
RFC 3986 character classification.

How Percent-Encoding Transforms Non-ASCII Characters into Hex Octets#

When encoding non-ASCII characters, the character is first converted to its UTF-8 byte representation. Each resulting byte is then written as `%` followed by two uppercase hexadecimal digits.

For example, the Euro symbol `€` is represented in UTF-8 as three bytes: `0xE2 0x82 0xAC`. When URL-encoded, `€` becomes `%E2%82%AC`.

JavaScript Pitfalls: encodeURI() vs encodeURIComponent()#

In JavaScript, developers frequently confuse `encodeURI` with `encodeURIComponent`:

  • `encodeURI()`: Encodes a complete, full URL. It preserves protocol schemes, domain slashes, and query marks (`:`, `/`, `?`, `&`, `#`), encoding only illegal characters like spaces or Unicode.
  • `encodeURIComponent()`: Encodes a single parameter value. It encodes EVERYTHING, including `/`, `?`, and `&`, preventing parameter values from breaking the overall URL structure.

The Space Dilemma: "%20" vs "+" Explained#

A frequent source of confusion is why spaces are sometimes encoded as `%20` and other times as `+`.

In standard RFC 3986 URI paths, a space MUST be encoded as `%20`. In legacy HTML `application/x-www-form-urlencoded` query strings (derived from early HTML form submissions), spaces are conventionally encoded as `+`. Modern web APIs prefer `%20` everywhere for strict RFC compliance.

Safe Query String Construction with URLSearchParams#

Rather than manually concatenating strings with template literals, always use the native `URL` and `URLSearchParams` browser APIs. They automatically handle correct percent-encoding, parameter escaping, and array serialization out of the box.

Key Takeaways & Best Practices
  • URL percent-encoding represents bytes as "%" followed by two hexadecimal digits.
  • Unreserved characters (A-Z, a-z, 0-9, -, _, ., ~) should never be percent-encoded.
  • Use encodeURIComponent() when encoding query parameter values; use encodeURI() for whole URLs.
  • Use native URLSearchParams to build query strings safely and prevent URL injection vulnerabilities.

Final Thoughts

Mastering RFC 3986 percent-encoding rules ensures reliable parameter transmission across web browsers, REST APIs, and search engines.

Encode, decode, parse, and debug complex URL strings instantly with Softnag’s in-browser URL Encoder & Decoder.

Related Technical Guides

View all 40 guides →