Encoding and Decoding are URI components is a usual use in website development while creating a GET request to API with parameters. Browsers automatically encode the URL i.e. it replace some special characters to other reserved characters . For eg: Space character ” ” is either converted to + or %20.
encodeURI()
The encodeURI() function is used to encode complete URI. This function encode the special character except (, / ? : @ & = + $ #) characters.
Syntax:
encodeURI( your_uri_string )
Example:
Output:
https://www.google.com/search?q=my%20inbox%20hub
encodeURIComponent()
The encodeURIComponent() function is used to encode some parts of URI. This function encodes the special characters. It also encodes the following characters: , / ? : @ & = + $ #
Syntax:
encodeURIComponent( uri_string_part )
Example:
Output:
my%20inbox%20hub
decodeURI()
The decodeURI() function is used to decode complete URI that generated by the encodeURL();
Syntax:
decodeURI( encoded_uri_string )
Example:
Output:
https://www.google.com/search?q=my inbox hub
decodeURIComponent()
The decodeURIComponent() function is used to decode URI component that generated by encodeURIComponent()
Syntax:
decodeURIComponent( encoded_uri_string_part )
Example:
Output:
my inbox hub