Three variants — and which to pick
- component — wraps
encodeURIComponent. Escapes everything exceptA-Z a-z 0-9 - _ . ! ~ * ' ( ). Use this when you're encoding a single piece of data that will sit inside a URL (query value, path segment, form field). - full URI — wraps
encodeURI. Leaves URL-structural characters intact (; , / ? : @ & = + $ #). Use this on an already-assembled URL where you only want to escape stray spaces or unsafe characters. - form — like
componentbut also turns spaces into+. This is what HTML forms submit asapplication/x-www-form-urlencoded.