1. encodeURI(): It is used to encode a given URI into UTF-8 format. The encodeURI() function takes URI (of type string) as a function parameter value and encodes a URI by replacing each instance of certain characters with one, two, three, or four escape sequences representing the UTF-8 encoding of the character.
Parameters: here, URI is the parameter that consists of a complete URI- Uniform Resource Identifier (of type string) which you want to encoded.
Return value: Above function will return a new string representing the ‘encoded URI’ for the given URI provided as the parameter to the function.
Note: In many browsers encodeURI() doesn’t encode many characters following are the set of special characters [ ~!@#$&*()=:/,;?+’ ] to encode these characters sets escape() is implemented separately. As well as English alphabets and numbers [ A-Z a-z 0-9 – _ . ! ~ * ‘ ( ) ] are not escaped by the encodeURI() for encoding complete URI string you can use encodeURI() and if you have for the part of URI string then you can use encodeURIComponent() to encode particular part of URI string.
2. decodeURI(): It is used to decode already previously encoded URI. This works in reverse, taking an encoded string and replacing the tokens with the normal characters. The decodeURI() function takes encodedURI (of type string) as a function parameter value and decodes a given encoded-URI, previously created by encodeURI() or by a similar routine.
Parameters: Here, encodedURI is the parameter that represents a complete, encoded URI (Uniform Resource Identifier). If you want decode the URI parameter must contain the URI in the encoded form only. Also, it throws an ‘URIError’ exception if the given parameter ‘encodedURI’ contains invalid character sequences.
Return value: above function will return a new string representing the decoded version of the given URI in the encoded form.
Note: Replaces each escape sequence in the encoded URI with the character that it represents, but does not decode escape sequences that could not have been introduced by encodeURI. It is used to decode the Cyrillic URLs as well; Cyrillic URL contains Cyrillic alphabet that looks similar to letters in the Latin alphabet (used in English), sometimes it is used to track the user and redirects them to fake websites.