URL Encoding 101: encodeURIComponent vs encodeURI vs rawuriencode
The complete guide to URL encoding in JavaScript and Python. Covers when to use encodeURIComponent, encodeURI, and how to handle special characters correctly.
URL Encoding 101: encodeURIComponent vs encodeURI vs rawuriencode
The Core Problem
URLs have a strict syntax. Only certain characters are allowed unencoded. Everything else must be percent-encoded.
โ
Valid in URLs: A-Z a-z 0-9 - _ . ~ !
โ Must be encoded: space, &, =, #, %, /, ?, :, @, $, &
Three JavaScript Functions
| Function | Encodes | Does NOT Encode |
|---|---|---|
encodeURIComponent() | Almost everything | A-Z a-z 0-9 - _ . ~ ! ' ( ) * |
encodeURI() | Reserved characters | A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ~ ! ' ( ) * # |
When to Use Which
// โ
Use encodeURIComponent for query parameter VALUES
const value = encodeURIComponent("Hello World"); // "Hello%20World"
const query = `name=${value}`; // "name=Hello%20World"
// โ
Use encodeURI for the full URL path (when you want to preserve /)
const url = encodeURI("https://example.com/path/to/page"); // preserved slashes
// โ NEVER use encodeURI for query strings (it leaves = ? & unencoded)
const bad = encodeURI("name=Hello World");
// "name=Hello%20World" โ but the = sign between name and value looks fine here
// But if value contains ?: @ & = +, it breaks
Common Mistakes
// โ Common mistake: mixing them up
const bad = "https://example.com/search?q=" + encodeURI("Hello World");
// Works, but semantically wrong
// โ
Correct
const good = "https://example.com/search?q=" + encodeURIComponent("Hello World");
// โ Forgetting to encode API keys or tokens
const apiKey = "abc123&secret=xyz"; // has & which will break the URL
const url = `https://api.example.com?key=${apiKey}`; // BROKEN
// The & in apiKey splits the parameter!
// โ
Always encode the value, not the URL
const url = `https://api.example.com?key=${encodeURIComponent(apiKey)}`;
Python Equivalent
from urllib.parse import urlencode, quote, quote_plus
# For query parameter values
params = { 'name': 'Hello World', 'city': 'Tainan' }
qs = urlencode(params) # "name=Hello+World&city=Tainan"
# For URL paths
path = quote('path/with/slashes') # "path%2Fwith%2Fslashes"
Special Cases
| Character | encodeURIComponent | encodeURI |
|---|---|---|
| Space | %20 | %20 |
! | %21 | ! |
# | %23 | %23 |
& | %26 | & |
= | %3D | = |
/ | %2F | / |
Key insight: Use encodeURIComponent for any value that goes inside a query string parameter. Use encodeURI for full URLs where you want to preserve path separators.
Free Newsletter
Level Up Your Dev Workflow
Get new tools, guides, and productivity tips delivered to your inbox.
Plus: grab the free Developer Productivity Checklist when you subscribe.
Found this guide useful? Check out our free developer tools.
Affiliate disclosure: Some links below are affiliate links โ we may earn a small commission at no extra cost to you. Learn more.
Recommended Tools & Resources
DigitalOcean
$200 credit for new users. Simple, affordable cloud hosting for developers.
GitHub Student Pack
Free access to 100+ developer tools. Perfect for students and new devs.
Vercel
Deploy frontend apps instantly. Free tier is generous for side projects.
DevPlaybook Products
Boilerplates, scripts & AI toolkits to 10x your dev workflow.