URL encoding: encodeURI, encodeURIComponent and spaces
Choose component or whole-URL percent encoding, understand %20 versus +, and avoid double encoding with working JavaScript examples.
Keep a query value from becoming URL syntax
A search string containing an ampersand must remain one value. In URL Encoder, leave URL component selected and encode hello world & team. The expected result is hello%20world%20%26%20team. Encoding the value prevents & from becoming another query separator.
const query = 'hello world & team'; const url = 'https://example.com/search?q=' + encodeURIComponent(query); // https://example.com/search?q=hello%20world%20%26%20team
Whole URLs need a different operation
encodeURI preserves separators such as :, /, ?, &, = and #. This can encode spaces in an already structured URL, but it cannot tell which ampersand belongs to a value. It does not validate a URL, remove dangerous schemes, or make arbitrary input safe to navigate. Component encoding escapes these separators; apply it to individual values.
encodeURI('https://example.com/?q=hello world');
// https://example.com/?q=hello%20world
encodeURIComponent('a/b?c=d&e=f');
// a%2Fb%3Fc%3Dd%26e%3Df
encodeURIComponent('café 😀');
// caf%C3%A9%20%F0%9F%98%80Spaces: %20 and + are different conventions
Both encodeURI and encodeURIComponent represent spaces as %20. application/x-www-form-urlencoded uses + for spaces. URLSearchParams implements that form convention. The tool's decoder uses decodeURI or decodeURIComponent, so it leaves a literal + unchanged; it does not perform form decoding.
new URLSearchParams({ q: 'hello world + team' }).toString();
// q=hello+world+%2B+team
new URLSearchParams('q=hello+world+%2B+team').get('q');
// hello world + team
decodeURIComponent('hello+world');
// hello+worldAvoid double encoding and match decoding modes
Encoding %20 a second time produces %2520 because the percent sign is encoded. Decode only the number of layers your protocol expects. The matching Whole URL decoder preserves escapes for reserved separators; Component decoder decodes them. Use URL Decoder to inspect these differences. Invalid percent escapes, invalid UTF-8 bytes and unpaired Unicode surrogates produce errors.
encodeURIComponent('%20'); // %2520
decodeURIComponent('%2520'); // %20
decodeURI('%2F%20'); // '%2F ' (slash stays encoded)
decodeURIComponent('%2F%20'); // '/ '
// decodeURIComponent('%E0%A4') throws URIErrorDecode percent-encoded text with URL Decoder
Try these examples in URL Encoder