Web development and URLs

What is URL Encoding?

URL encoding, also known as percent encoding, is the process of converting special characters in URLs into a format that can be safely transmitted over the internet. URLs can only contain certain characters from the ASCII set, so any other characters must be encoded.

For example, spaces become %20, and special characters like &, =, and ? have special meanings in URLs and must be encoded when used as data.

Why URL Encoding is Necessary

URLs were designed to be simple and portable. They can only safely contain:

  • Letters (A-Z, a-z)
  • Numbers (0-9)
  • A few special characters: - _. ~ (unreserved characters)

Any other characters—including spaces, non-ASCII characters (like é, 日, or emoji), and reserved characters with special URL meanings—must be encoded to prevent misinterpretation.

Reserved vs Unreserved Characters

Reserved Characters (Have Special Meaning in URLs)

: /? # [ ] @! $ & ' ( ) * +,; =

These characters have specific purposes in URLs:

  • : Separates protocol from host
  • / Separates path segments
  • ? Starts query string
  • & Separates query parameters
  • = Separates parameter names from values
  • # Indicates fragment (anchor)

Unreserved Characters (Safe to Use)

A-Z a-z 0-9 - _. ~

How Percent Encoding Works

When encoding a character, it's converted to its UTF-8 byte representation, then each byte is written as % followed by two hexadecimal digits.

Original: Hello World!
Encoded: Hello%20World%21
Original: search=user@email.com
Encoded: search=user%40email.com

encodeURI vs encodeURIComponent

JavaScript provides two main functions for URL encoding, and understanding when to use each is crucial.

encodeURI()

Use encodeURI() when encoding a complete URL. It encodes most special characters but preserves URL structure characters like :, /, ?, and &.

const url = "https://example.com/search?q=hello world";
const encoded = encodeURI(url);
// "https://example.com/search?q=hello%20world" // Notice::, /, and? are NOT encoded

encodeURIComponent()

Use encodeURIComponent() for encoding individual parameter values. It encodes all special characters except - _.! ~ * ' ( ).

const param = "user@email.com?test=true";
const encoded = encodeURIComponent(param);
// "user%40email.com%3Ftest%3Dtrue" // Notice: @,?, and = are all encoded

Which One Should You Use?

ScenarioUse
Encoding a complete URLencodeURI()
Encoding query parameter valuesencodeURIComponent()
Encoding user input for URLsencodeURIComponent()
Building API requestsencodeURIComponent()

Common Mistakes and Gotchas

1. Double Encoding

Encoding the same string twice is a common mistake that leads to broken URLs:

❌ Wrong:

const param = "hello world";
const encoded = encodeURIComponent(encodeURIComponent(param));
// "hello%2520world" (double encoded!)

2. Using encodeURI() for Parameters

❌ Wrong:

const query = "user@email.com";
const url = `https://api.com/search?email=${encodeURI(query)}`;
// Characters like @ won't be encoded!

✅ Correct:

const url = `https://api.com/search?email=${encodeURIComponent(query)}`;

3. Not Encoding Plus Signs

Both functions don't encode +, but in query strings, + is interpreted as a space. If you want a literal plus sign, encode it as %2B.

Using URLSearchParams (Modern Approach)

The URLSearchParams API handles encoding automatically:

const params = new URLSearchParams({ name: "John Doe", email: "john@example.com"
}); const url = `https://api.com/search?${params.toString()}`;
// Encoding handled automatically!

Best Practices

  1. Always use encodeURIComponent(): For user-provided data in URLs
  2. Decode on the server: Always decode URL parameters server-side
  3. Validate after decoding: Check for malicious input or injection attempts
  4. Use URLSearchParams: For modern JavaScript applications
  5. Be consistent: Encode the same way throughout your application
  6. Test with real data: Don't assume ASCII-only input

Try CoderFile.io URL Encoder

Encode and decode URLs instantly with automatic query parameter parsing. Perfect for testing and debugging URL encoding issues.

Encode URLs Now →

Conclusion

URL encoding is a fundamental aspect of web development that developers encounter daily. Understanding the difference between encodeURI() and encodeURIComponent(), knowing when to use each, and avoiding common pitfalls will help you build more robust web applications.

Whether you're building API clients, processing form submissions, or constructing dynamic URLs, proper URL encoding ensures your application handles special characters and international content correctly.

Related Tools & Resources