Component vs full URL

encodeURIComponent escapes every character with syntactic meaning in a URL — including :, /, ?, #, & and =. Use it for a query value or a path segment. encodeURI leaves those structural characters alone and is appropriate for a complete URL you want to clean up.

Encoding a full URL with encodeURIComponent turns https://example.com/a?b=c into https%3A%2F%2Fexample.com%2Fa%3Fb%3Dc, which is a valid query value but no longer a URL.

javascript
encodeURIComponent('a b&c')   // 'a%20b%26c'
encodeURI('https://x.com/a b') // 'https://x.com/a%20b'

Characters that must be escaped

CharacterEncodedReason
space%20 or ++ means space only in form encoding
&%26Query parameter separator
=%3DKey/value separator
?%3FQuery string start
#%23Fragment start
/%2FPath separator (unless intended)
%%25Percent itself must be escaped

A literal % must be encoded as %25. A stray percent sign makes the URL invalid and some parsers silently drop the rest.

Plus signs and form encoding

application/x-www-form-urlencoded uses + for spaces, which is why query strings from HTML forms look different from encodeURIComponent output. When decoding, treat + as a space only if you know the source used form encoding; otherwise a literal plus in a value will be lost.

Double-encoding

Encoding an already-encoded string produces %2520 where %20 was intended. This is the most common URL bug in redirect chains and signed URLs. Decode once, inspect, then encode once.