Difference Between encodeURI and encodeURIComponent in JavaScript
Last Updated :
27 Jun, 2024
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 differences between these two functions is essential for proper URI encoding in web applications.
What is encodeURI?
The encodeURI
function in JavaScript encodes a complete URI (Uniform Resource Identifier). It preserves the URI structure by not encoding characters that have special meanings in a URI, such as :
, /
, ?
, &
, #
, etc. This function is useful when you want to encode an entire URI without altering its structure.
Syntax:
encodeURI(URI)
Example: In the example, the space character in the query parameter is encoded as %20 but the ? and = characters are preserved.
JavaScript
encodeURI(URI)
let uri = "https://www.example.com/search?query=hello world";
let encodedURI = encodeURI(uri);
console.log(encodedURI);
// "https://www.example.com/search?query=hello%20world"
Output:
"https://www.example.com/search?query=hello%20world"
What is encodeURIComponent
?
The encodeURIComponent
is a JavaScript function used to encode a URI component. It encodes all characters that are not allowed in a URI component, including :
, /
, ?
, &
, #
, etc. This function is useful when you want to encode individual parts of a URI, such as query parameters, without preserving special characters.
Syntax:
encodeURIComponent(uriComponent)
Example: The example below shows the encodeURIComponent
.
JavaScript
encodeURIComponent(uriComponent)
let queryParam = "hello world";
let encodedQueryParam = encodeURIComponent(queryParam);
console.log(encodedQueryParam);
// "hello%20world"
Output:
hello%20world
Difference Between encodeURI and encodeURIComponent
Characteristics | encodeURI | encodeURIComponent |
---|
Purpose | Encodes a complete URI | Encodes a URI component |
---|
Characters Not Encoded | /, ?, &, #, ,, @, = | All special characters including : /, ?, &, # |
---|
Usage Example | encodeURI("https://example.com/?a=b&c=d") | encodeURIComponent("a=b&c=d") |
---|
Example Output | https://example.com/?a=b&c=d | a%3Db%26c%3Dd |
---|
Conclusion
In summary, encodeURI
is used for encoding an entire URI while preserving characters that have special meanings in the URI. In contrast, encodeURIComponent
is used for encoding individual components of the URI, encoding all special characters. Knowing when to use each function is crucial for proper URI encoding and ensuring that your web applications handle URIs correctly.
Similar Reads
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
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 Encryption and Encoding Encryption and Encoding are terms often interchanged and used incorrectly, but they have distinct differences. Anyone dealing with communication networks or data security needs to be aware of these distinctions. In this article, we will learn the difference between the two terms, encryption and enco
4 min read
What are the encodeURI() and decodeURI() in JavaScript ? 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
3 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