You are debugging a broken API call. The URL looks fine in your browser, but the server returns 400. You stare at the query string: ?q=hello world&filter=active. There is your problem — that space.
URL encoding is one of those things every developer encounters but few fully understand. Let us fix that.
The Two JavaScript Functions (and Why Both Exist)
encodeURI() is for entire URLs. It preserves characters that have structural meaning: :, /, ?, &, #. Use it when you have a complete URL and just need to make it safe.
encodeURIComponent() is for individual parameter values. It encodes everything except A-Z a-z 0-9 - _ . ! ~ * ' ( ). The & in your filter value? Encoded. The = in your base64 token? Encoded.






