URL Encode in JavaScript — encodeURIComponent Guide
Quick Answer: In JavaScript, use encodeURIComponent() for query parameter values and encodeURI() for full URLs. encodeURIComponent encodes everything except A-Z a-z 0-9 - _ . ~ while encodeURI preserves URL structure characters like : / ? # & =.
FAQ
What is the difference between encodeURI and encodeURIComponent?
encodeURI() preserves URL-safe characters (://?#&=). encodeURIComponent() encodes everything for use inside a query parameter value.
When do I use encodeURIComponent?
Use it for individual query parameter values: `?search=${encodeURIComponent(userInput)}`. Never for the full URL -- that would break the URL structure.