![]() |
VOOZH | about |
The encodeURI() and decodeURI() functions in JavaScript are used to handle URI (Uniform Resource Identifier) encoding and decoding. They ensure that URIs are properly formatted for web usage, converting characters that may cause issues into a valid, encoded format.
The encodeURI() function encodes a URI by replacing certain characters with their UTF-8 escape sequences. It preserves characters that are valid in a URI, like :, /, ?, and #.
https://example.com/query?name=Amit%20Kumar&age=25
encodeURI() converts the space in "Amit Kumar" into %20 but leaves the ?, =, and & characters intact, as they are part of the URI syntax.
The decodeURI() function decodes an encoded URI by replacing escape sequences with their original characters.
https://example.com/query?name=Amit Kumar&age=25
decodeURI() converts the %20 back into a space, restoring the original URI.
In addition to encodeURI() and decodeURI(), JavaScript provides the encodeURIComponent() and decodeURIComponent() functions, which operate on individual components of a URI.
Encodes a URI component (such as query string parameters or path segments) and encodes characters like &, =, ?, and others.
Amit%20Kumar
encodeURIComponent() encodes the space in "Amit Kumar" into %20, as it treats each component as part of a larger URI.
Decodes a URI component back into its original format.
Amit Kumar
decodeURIComponent() decodes %20 back into a space, restoring the original string.
| Function | Purpose | Encodes/Decodes |
|---|---|---|
| encodeURI() | Encodes a full URI, leaving URI delimiters (:, /, ?, &) intact | Encodes non-URI characters |
| decodeURI() | Decodes a full URI, reversing percent-encoding for non-URI characters | Decodes percent-encoded URI |
| encodeURIComponent() | Encodes individual URI components (query parameters, path segments) | Encodes all characters |
| decodeURIComponent() | Decodes individual URI components back into their original form | Decodes percent-encoded component |