What is URL Encoding (Percent-Encoding)?
URL Encoding (technically called percent-encoding) is a mechanism for encoding information in a Uniform Resource Identifier (URI) under RFC 3986. Characters outside the allowed ASCII character set (such as spaces, non-Latin characters, and control codes) or characters with reserved meanings (such as ?, &, =, and #) are replaced with a percent sign followed by two hexadecimal digits (e.g. space becomes %20).
encodeURI vs. encodeURIComponent
In JavaScript, developers frequently confuse encodeURI and encodeURIComponent:
- encodeURIComponent(): Encodes everything except
A-Z a-z 0-9 - _ . ! ~ * ' ( ). Use this when encoding individual query parameter values (e.g.?user=John%20Doe). - encodeURI(): Assumes the input is a complete URL and leaves protocol separators, slashes, and query indicators (
: / ? # & =) unencoded. Use this on entire URL strings.
