What is URL Encoding?
URL encoding — also called percent-encoding — is the mechanism that converts characters into a format safe for transmission inside a Uniform Resource Locator. URLs can only travel across the internet using the ASCII character set, which means any character outside that set needs to be transformed. The percent-encoding scheme defined in RFC 3986 handles this by replacing unsafe characters with a percent sign followed by two hexadecimal digits representing the character's byte value. A space becomes %20, an ampersand becomes %26, and a forward slash becomes %2F. Without this translation layer, browsers and servers would misinterpret special characters as structural delimiters rather than literal data.
The need for encoding comes down to one thing: URLs have a grammar. Characters like the colon, question mark, hash, and forward slash carry specific meaning inside a URL's structure. The colon separates the scheme from the authority. The question mark introduces a query string. The hash marks a fragment identifier. RFC 3986 splits the full ASCII range into two groups — reserved characters and unreserved characters. Reserved characters include :/?#[]@!$&'()*+,;= and they serve as delimiters in the URL syntax. Unreserved characters are the uppercase and lowercase letters A-Z and a-z, the digits 0-9, and four symbols: hyphen, period, underscore, and tilde. Unreserved characters pass through without any encoding. Everything else gets percent-encoded.
A common mistake is encoding an entire URL blindly. You should only encode the parts that carry user data — path segments, query parameter keys, and query parameter values. Encoding the structural delimiters themselves breaks the URL. That is exactly why JavaScript gives you two different functions: encodeURI handles a full URI and leaves structural characters intact, while encodeURIComponent encodes almost everything and is meant for individual components like a single query value. Knowing which function to reach for saves you hours of debugging malformed requests. The distinction matters more than most developers realize until they hit a bug in production.