Encode special URL characters for safe transmission or decode them back to original.
URL encoding, also known as percent-encoding, is a mechanism for encoding information in a Uniform Resource Identifier (URI). It replaces unsafe ASCII characters with a '%' followed by two hexadecimal digits representing the character's byte value. This ensures URLs remain valid and can be transmitted over the internet without issues.
URL encoding converts characters into a format that can be transmitted over the internet. Safe characters (A-Z, a-z, 0-9, -, _, ., ~) remain unchanged. All other characters are converted to their UTF-8 byte sequence, with each byte represented as %XX where XX is the hexadecimal value.
Hello World
Hello%20World
name=John&age=30
name%3DJohn%26age%3D30
https://example.com/search?q=test
https%3A%2F%2Fexample.com%2Fsearch%3Fq%3Dtest
You should URL encode whenever you're including user input or special characters in URLs, especially in query parameters or path segments.
encodeURI encodes a complete URI and preserves characters like :, /, ?, and #. encodeURIComponent encodes everything except alphanumeric characters and - _ . ~ making it suitable for encoding query parameter values.