What is the difference between encodeURIComponent and encodeURI in JavaScript?
encodeURIComponent encodes everything except unreserved characters (A–Z a–z 0–9 - _ . ~), making it suitable for encoding a single query parameter value or path segment. It encodes: & → %26, = → %3D, / → %2F, + → %2B, ? → %3F, # → %23. encodeURI encodes the same characters but preserves structural URL characters (: / ? # [ ] @ ! $ & ' ( ) * + , ; =) so the result remains a valid URL. Rule: use encodeURIComponent for parameter values — encodeURIComponent('hello world & page=2') → 'hello%20world%20%26%20page%3D2'. Use encodeURI only to lightly sanitize a complete URL string you already control.