URL Encode Space Character
Learn how to URL encode space characters. Understand the difference between %20 and + encoding, when to use each, and common pitfalls.
Character
Encoded
%20 or +
Detailed Explanation
The space character is one of the most frequently encoded characters in URLs, and it has two valid encoded forms: %20 and +. Understanding when to use each is critical for building correct URLs.
Percent-encoded form: %20 is the standard percent-encoding for a space (ASCII code 32, which is 0x20 in hexadecimal). This is the form defined by RFC 3986 for general URI encoding.
Plus-sign form: + is an alternative representation of a space, but only within the query string portion of a URL. This convention comes from the application/x-www-form-urlencoded content type, defined in the HTML specification for form submissions.
JavaScript behavior:
encodeURIComponent(" ")returns%20encodeURI("hello world")returnshello%20worldnew URLSearchParams({q: "hello world"}).toString()returnsq=hello+world
The distinction matters. If you are constructing a URL path like /search/hello world, you must use %20: /search/hello%20world. Using + in a path segment would be interpreted as a literal plus sign, not a space.
In query parameters, both forms are generally accepted by servers. However, when building form-encoded bodies for POST requests, the + convention is standard. Most server-side frameworks decode both forms automatically in query strings, but being consistent avoids subtle bugs.
Common pitfall: Developers sometimes manually replace spaces with + across an entire URL, breaking path segments. Always use the appropriate encoding function for the URL component you are building. For paths, use encodeURIComponent() which produces %20. For query parameters in form-encoded format, use URLSearchParams which produces +.
Use Case
Encoding search queries in URL parameters, such as building a Google search link like https://google.com/search?q=hello+world or an API call with spaces in parameter values.