Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

What are the encodeURI() and decodeURI() in JavaScript ?

  • Difficulty Level : Easy
  • Last Updated : 19 Oct, 2021

URLs and URIs are designed to locate/identify resources available over the internet, anything that uniquely identifies a resource is its URI, such as id, name. A URL specifies a resource and its access protocol. All URLs are URIs, but not all URIs are URLs. URI can only have certain characters from the standard 128 ASCII character set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL. In JavaScript, we have two special functions to serve these tasks of encoding and decoding the URIs. encodeURI() and decodeURI() these functions are introduced to encode/decode non-English characters, such as Latin, Greek letters used in URI (Uniform Resource Identifier). Also, it is helpful to encode special characters, replace whitespaces in URI; sometimes these special characters or whitespaces can delimit the URI.

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.



    const uri = 'γεια';
    const encoded = encodeURI(uri);
    console.log("Encoded URI - ", encoded);
    // Expected output: 
    try {
        console.log("Decoded URI - ", decodeURI(encoded));
        // Expected output: 
        // "γεια"
    } catch (e) {



Output of encodeURI and decodeURI

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!