What Is URL Encoding?
URL encoding (also called percent-encoding) replaces unsafe or reserved characters in a URL with a % sign followed by two hexadecimal digits. For example, a space becomes %20. This ensures URLs are transmitted correctly across the internet without ambiguity.
encodeURIComponent vs. encodeURI
encodeURIComponent encodes everything except alphanumerics and - _ . ! ~ * ' ( ). Use it for query parameters. encodeURI preserves URL-structural characters like : / ? # [ ] @, making it suitable for encoding full URLs.
Common Use Cases
- Encoding query string parameters containing special characters
- Preparing strings for use in API requests and webhooks
- Debugging percent-encoded URLs from server logs
- Encoding file paths with spaces or unicode characters
- Converting between URL-safe and human-readable formats