What Is URL Encoding? A Complete Beginner’s Guide to Percent-Encoding
Why do spaces turn into %20 and ampersands break query parameters? Learn the mechanics of URL percent-encoding (RFC 3986) with practical developer examples.
Introduction#
Have you ever clicked a link or submitted a search form and noticed that the URL turned into a bizarre string of percent signs and hexadecimal numbers?
https://example.com/search?q=shoes%20%26%20socks%21What happened to the spaces, the ampersand (&), and the exclamation mark (!)?
This process is known as URL Encoding (officially termed Percent-Encoding in IETF RFC 3986). It is the mechanism that ensures web browsers and HTTP web servers agree unambiguously on where a URL path ends, where query parameters begin, and which characters represent raw textual data.
In this guide, we demystify how URL encoding functions, examine the difference between reserved and unreserved characters, and demonstrate how to handle encoding in modern JavaScript.
Table of Contents
Why Do URLs Need Special Encoding?#
A Uniform Resource Identifier (URI) must use a strictly defined set of US-ASCII characters. Furthermore, certain characters have predefined structural meanings within a URL:
?) separates the path from query parameters.&) separates individual key-value pairs.=) assigns a value to a parameter key.#) marks the beginning of an internal document anchor.If a user searches for the company "Barnes & Noble", submitting the raw string ?company=Barnes & Noble breaks the URL parser: the server interprets Barnes as the company name, and thinks Noble is a brand new parameter key without a value!
To pass literal symbols safely, those characters must be percent-encoded.
The Anatomy of Percent-Encoding#
Percent-encoding replaces unsafe, non-ASCII, or reserved characters with a percent sign (%) followed by the two-digit hexadecimal representation of the character's UTF-8 byte value.
Practical Conversion Example#
%20.Common URL Encoding Reference Table#
| Character | Literal Symbol | Percent-Encoded Hex Code | Reason for Encoding |
|---|---|---|---|
| Space | | %20 (or +) | Invalid in raw URI paths |
| Exclamation Mark | ! | %21 | Sub-delimiter |
| Double Quote | " | %22 | Unsafe delimiter |
| Hash / Pound | # | %23 | Fragment identifier reserved |
| Ampersand | & | %26 | Query parameter separator reserved |
| Forward Slash | / | %2F | Path segment separator reserved |
| Colon | : | %3A | Scheme & port separator reserved |
| Equals | = | %3D | Key-value separator reserved |
| Question Mark | ? | %3F | Query string initiator reserved |
Difference Between %20 and '+' for Spaces#
Developers frequently ask why spaces are sometimes encoded as %20 and other times as +:
%20 (Standard RFC 3986): The universal standard for all URI paths, filenames, and modern API query strings.+ (HTML Form Spec / application/x-www-form-urlencoded): A legacy convention used specifically when submitting standard HTML <form> POST bodies or GET query parameters.encodeURI vs encodeURIComponent in JavaScript#
JavaScript provides two native global encoding functions that serve completely different purposes:
const fullUrl = "https://example.com/search?q=fish & chips";
// 1. encodeURI: Preserves URL structural characters (?, =, &, /)
encodeURI(fullUrl);
// Returns: "https://example.com/search?q=fish%20&%20chips" (ampersand NOT encoded!)
// 2. encodeURIComponent: Encodes EVERYTHING (use for query parameter values)
const queryParam = "fish & chips";
const safeUrl = "https://example.com/search?q=" + encodeURIComponent(queryParam);
// Returns: "https://example.com/search?q=fish%20%26%20chips" (ampersand safely encoded!)Helpful In-Browser Developer Utilities#
Frequently Asked Questions#
What happens if I double-encode a URL?#
If %20 is encoded a second time, the % sign becomes %25, resulting in %2520. Double-encoding is a common cause of broken links and database query bugs.
Does URL encoding support emojis and non-English characters?#
Yes! Emojis (like 🚀) and non-Latin scripts (like Japanese or Arabic) are first converted to their multi-byte UTF-8 representations, and each byte is percent-encoded (e.g., 🚀 becomes %F0%9F%9A%80).
Summary#
URL encoding ensures that complex data travels safely through the internet's routing infrastructure without confusing web servers. Using encodeURIComponent for query parameter values prevents syntax breaking and protects application stability.
Related Reading
JSON vs XML vs YAML: Differences, Syntax Comparisons, and Modern Use Cases
Which data serialization format should you use? We compare JSON, XML, and YAML across readability, parser speed, schema validation, and real-world API architecture.
Developer Tools & ProgrammingWhat Is Base64 Encoding and How Does It Work? Complete Visual Guide
Ever wondered what Base64 strings really are? Learn how 8-bit binary data is transformed into 6-bit ASCII characters for safe email and web transmission.

