URL Encoding Explained
URLs can only contain a limited set of ASCII characters. Spaces, symbols, and non-Latin characters must be percent-encoded — converted to a %XX format where XX is the hexadecimal byte value.
1. Why does a space become %20?
The space character (ASCII 32) has hexadecimal value 20, hence %20. Some older systems also use '+' for space in query strings.
2. What's the difference between encodeURI and encodeURIComponent?
encodeURI encodes a full URL and leaves characters like / and ? intact. encodeURIComponent encodes query-string values and escapes everything including those characters. We use encodeURIComponent.
3. Why are my Chinese/Arabic characters encoded?
Non-ASCII characters are first UTF-8 encoded, then each byte is percent-encoded. This is the correct RFC 3986 standard.
4. What causes 'Error: invalid percent-encoded string'?
This occurs when decoding a malformed sequence like a lone % sign not followed by two hex digits.
5. Is this useful for SEO?
Yes. Understanding URL encoding helps diagnose tracking link issues, canonical tag problems, and search query parameter handling in analytics.