What are the encodeURI() and decodeURI() in JavaScript ?
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.
Syntax:
encodeURI(URI)
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.
Syntax:
decodeURI(encodedURI)
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.
Example:
JavaScript <script> const uri = 'https://www.geeksforgeeks.org/?x=γεια'; const encoded = encodeURI(uri); console.log("Encoded URI - ", encoded); // Expected output: //"https://www.geeksforgeeks.org/?x=%CE%B3%CE%B5%CE%B9%CE%B1" try { console.log("Decoded URI - ", decodeURI(encoded)); // Expected output: // "https://www.geeksforgeeks.org/?x=γεια" } catch (e) { console.error(e); } </script>
Output:
Output of encodeURI and decodeURI Similar Reads
JavaScript encodeURI(), decodeURI() and its components Functions
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. 1. encodeURI() FunctionThe encodeU
3 min read
How to Encode and Decode a URL in JavaScript?
Encoding and decoding URLs in JavaScript is essential for web development, especially when making GET requests with query parameters. This process ensures special characters in URLs are correctly interpreted by the server. For instance, spaces are converted to %20 or + in URLs. This guide covers how
5 min read
JavaScript | Encode/Decode a string to Base64
To encode or decode strings in JavaScript, we can use the built-in functions provided by the language. These functions help in encoding special characters in a URL or decoding encoded strings back to their original form. 1. btoa() MethodThis method encodes a string in base-64 and uses the "A-Z", "a-
6 min read
Difference Between encodeURI and encodeURIComponent in JavaScript
The encodeURI and encodeURIComponent functions are used to encode a URI by transforming characters that could otherwise cause issues when included in a URI. While both functions are used for encoding, they serve different purposes and encode different sets of characters. Understanding the difference
2 min read
What are the builtin strings in JavaScript ?
A sequence of letters, special characters, numbers, etc., or a combination of all of them is known as a string. Strings are created by enclosing the string characters within a single quote (') or within double quotes ("). Syntax: var myString = 'Good Morning123!'; // Single quoted string var myStrin
3 min read
Convert an Array to JSON in JavaScript
Given a JavaScript Array and the task is to convert an array to JSON Object. Below are the approaches to convert an array to JSON using JsvaScript: Table of Content JSON.stringify() methodObject.assign() methodJSON.stringify() methodThe use of JSON is to exchange data to/from a web server. While sen
2 min read
Which functions are used to encode and decode JSON file in PHP ?
JSON stands for JavaScript Object Notation. Like XML, it is a text-based format for the exchange of data which is easier to read and write and it is lighter than other formats. JSON is based on two basic structures namely Objects and Arrays. Parsing JSON data in PHP: There are built-in functions in
2 min read
Convert a JavaScript Enum to a String
Enums in JavaScript are used to represent a fixed set of named values. In JavaScript, Enumerations or Enums are used to represent a fixed set of named values. However, Enums are not native to JavaScript, so they are usually implemented using objects or frozen arrays. In this article, we are going to
2 min read
Difference between decodeURIComponent() and decodeURI() functions in JavaScript
Both decodeURI() and decodeURIComponent() are Javascript global functions that are used for decoding encoded URI (Uniform Resource Identifier). JavaScript decodeURI() function: It decodes a string previously encoded by the encodeURI() function. It returns a decoded URI by replacing each UTF-8 escape
2 min read
When we use Escape Instead of encodeURI / encodeURIComponent in JavaScript ?
A URL consists of many characters, both special and unique. Unique characters include those that are not plain standard such as spaces etc. So it means that we need to encode characters in UTF-8. So if we have a string such as: "https://www.gfg.com/what is html?" then UTF-8 will encode it as "https:
3 min read