Looking for a quick answer on a specific case?
- How to Encode Spaces in URLs
- How to Encode Special Characters in URLs
- How to Encode Email Addresses in URLs
- How to URL Encode UTF-8 Characters
- URL Encoding in JavaScript
- URL Encoding in C#
- URL Encoding in Go
Component vs. full URI encoding
encodeURIComponent escapes every character that isn't valid in a single URL segment — including &, =, ?, and /. Use it when encoding a value that will be placed inside a query string or path segment, like a search term.
encodeURI assumes you're encoding a whole URL and leaves characters that are structurally meaningful in a URL (:, /, ?, &, =) untouched, only escaping spaces and other genuinely unsafe characters.
FAQ
Which one should I use for a query parameter value?
Component mode, almost always. If you encode a full URL with component mode you'll end up double-escaping the :// and path separators, breaking the URL.
What about escape() or URLSearchParams?
escape() is deprecated and genuinely breaks on emoji and other non-Latin-1 characters — avoid it entirely. URLSearchParams is usually the better choice for building a query string, but it encodes a slightly different character set than encodeURIComponent does. See URL Encoding in JavaScript for the verified differences between all four.