Encode special characters for URLs or decode percent-encoded strings. Supports both encodeURI and encodeURIComponent modes.
URL encoding (also called percent encoding) replaces special characters in a URL with a percent sign (%) followed by two hexadecimal digits. For example, a space becomes %20. This ensures URLs are valid and transmitted correctly.
Encode (encodeURI) encodes a full URI but preserves characters like :, /, ?, #, and &. Encode Component (encodeURIComponent) encodes everything except letters, digits, and - _ . ~, which is what you want for query parameter values.
URL encode whenever you pass user input or special characters in URLs, query strings, or form data. This prevents broken URLs and potential security issues like injection attacks.
In URL encoding, spaces are represented as %20. In form data (application/x-www-form-urlencoded), spaces become +. Both are valid representations of a space in different contexts.