Short answer: for one value — a search term, a filename, a query parameter — choose component; for a whole address, choose whole URL. Decoding shows the text plus a breakdown of the URL, so you can check what the server will actually receive.
Two modes, because one is wrong half the time
encodeURIComponent('a b&c') returns a%20b%26c: everything reserved is escaped, so the result is safe as a single value. encodeURI() returnshttps://e.com/a%20b?x=1&y=2 for a whole URL, leaving the syntax characters alone. Choosing the wrong one either breaks the address into pieces or leaves an& inside a parameter that then splits it.
What the breakdown shows
- Protocol, host and port, kept separate from the path.
- The path as the browser normalises it.
- Query parameters in order, including repeated keys such as
tag=one&tag=two. - The fragment, which never reaches the server.
- Warnings for http://, embedded credentials and punycode hostnames.
Warnings worth reading
Credentials in a URL (https://user:pass@host) end up in server logs, proxy logs and the Referer header. A plain http:// address sends its query string — which may include a session token — in clear text. The breakdown flags both rather than showing a tidy table and letting you assume it is fine.