Encoding & Security 4 min read Oct 07, 2026 FormatKit Engineering 6 views

URL Encoding vs Decoding: Handling Special Characters in Query Strings

Demystify percent-encoding (RFC 3986). Learn why spaces become %20 or +, how encodeURI differs from encodeURIComponent, and how to avoid broken query parameters.
Quick Answer • Key Takeaway
URL encoding (percent-encoding) converts unsafe characters into a % followed by two hexadecimal digits. Use encodeURIComponent() for individual query parameter values to escape &, =, ?, and spaces. Use encodeURI() only when encoding an entire complete URL without breaking protocol slashes.
Guide Sections

Why URLs Require Encoding

Uniform Resource Locators (URLs) are restricted to a limited subset of US-ASCII characters. Characters like spaces, ampersands (&), question marks (?), and equals signs (=) serve reserved structural purposes in HTTP query strings. If user input contains these characters without escaping, browser and server parsers misinterpret the parameters.

const query = "shoes & socks/red";

// ❌ encodeURI keeps slashes and ampersands intact:
console.log(encodeURI(query)); 
// Output: "shoes%20&%20socks/red" (Breaks query parameter parsing!)

// ✅ encodeURIComponent escapes ALL reserved delimiters:
console.log(encodeURIComponent(query));
// Output: "shoes%20%26%20socks%2Fred" (Safe for query values!)

Encode or Decode URLs Instantly

Safely encode query parameters or decode percent-encoded URLs and form bodies.

URL Encoder / Decoder →

Frequently Asked Questions

Why does a space sometimes encode as + and sometimes as %20?
RFC 3986 specifies %20 for standard percent-encoding across all URI parts. The + symbol is an older convention defined for HTML form submissions (application/x-www-form-urlencoded) inside query strings. Both are widely supported by servers.
What happens if a URL is encoded twice?
Double-encoding occurs when an already-encoded percent symbol (%) is encoded again into %25. For example, %20 becomes %2520, causing search queries and file paths to resolve incorrectly.
On This Page
100% In-Browser Privacy

FormatKit tools and interactive code examples run completely in your client browser sandbox. We never store, log, or transmit your code, tokens, or confidential API payloads.