Difference between decodeURIComponent() and decodeURI() functions in JavaScript
Last Updated :
29 Dec, 2022
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 sequence with the characters it represents.
Syntax:
decodeURI(encodeURI(x));
Parameters: It contains a single parameter that includes a string previously encoded by the encodeURI() function and hence result will be x again.
Example: This example uses decodeURI() function.
HTML
<script type="text/javascript">
var decodeText1 = decodeURI('http://www.testing.com/');
console.log(decodeText1);
var decodeText2 = decodeURI('https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttp%2Fwww.testing.com%2F');
console.log(decodeText2);
</script>
Output:
http://www.testing.com/
https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttp%2Fwww.testing.com%2F
JavaScript decodeURIComponent() function: It decodes a string previously encoded by the encodeURIComponent() function. It returns a decoded URI Component by replacing each UTF-8 escape sequence with the characters it represents. It can decode any value between %00 and %7F.
Syntax:
decodeURIComponent(encodeURIComponent(x));
Parameters: Single parameter that includes a string previously encoded by encodeURIComponent() and hence result will be x again.
Example: This example is on decodeURIComponent()
HTML
<script type="text/javascript">
var decodeText1 = decodeURIComponent(
'http://www.testing.com/');
console.log(decodeText1);
var decodeText2 = decodeURIComponent(
'https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttp%2Fwww.testing.com%2F');
console.log(decodeText2);
</script>
Output:
http://www.testing.com/
http://www.testing.com/
Note:Both functions throw URIError indicating that one or more of the escape sequences in string is malformed and cannot be correctly decoded.
Difference between decodeURIComponent() and decodeURI() Function:
- decodeURI(): It takes encodeURI(url) string so it cannot decoded characters (, / ? : @ & = + $ #)
- decodeURIComponent(): It takes encodeURIComponent(url) string so it can decode these characters.
- decodeURI(): It takes encodeURI(url) string as parameter and returns the decoded string.
- decodeURIComponent(): It takes encodeURIComponent(url) string as parameter and returns the decoded string.
- decodeURI("%41"): It returns "A"
- decodeURIComponent("%41") It returns "A"
- decodeURI("%26"): It returns "%26"
- decodeURIComponent("%26"): It returns "&"
Similar Reads
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
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 encodeUR
2 min read
Difference between Anonymous and Named functions in JavaScript In JavaScript or in any programming language per say, functions, loops, mathematical operators and variables are the most widely used tools. This article will tell you about the difference between anonymous functions and named functions. We will discuss all the required concepts in this article to k
4 min read
What is the difference between children and childNodes in JavaScript? DOM childNodes Property: The childNodes property is a property of Node in Javascript and is used to return a Nodelist of child nodes. Nodelist items are objects, not strings and they can be accessed using index numbers. The first childNode starts at index 0. Syntax element.childNodes DOM children Pr
2 min read
Difference between unescape() and escape() functions in JavaScript In this article, we will learn about the escape() & unescape() functions in JavaScript. We will understand the purpose of using both these functions through the example. Later in this article, we will discuss the difference between escape() & unescape() functions. Let's discuss the escape()
4 min read
Difference between First-Class and Higher-Order Functions in JavaScript Understanding the difference between first-class and higher-order functions in JavaScript is important. These are big concepts in programming, especially in the kind of coding used for making websites. This article is all about explaining what they are, how they are used, and why they are so importa
3 min read