Skip to content

JavaScript – encodeURI() vs encodeURIComponent()

The purpose of the tutorial is to show you the differences between encodeURI() and encodeURIComponent() functions.

encodeURIComponent()

This method will encode the given string , and if you pass the encoded string to decodeURIComponent(), it will return the original string.

This method will not encode following characters ~!*()'" and it will encode all the characters along with pure URI characters(/ ? : @ & = + $ , – _ . ! ~ * ‘ ( ) #).

This method is helpful, if query string contains URLs.

encodeURIComponent("https://arjunphp.com/php tutorials#iterator loops");
output: "https%3A%2F%2Farjunphp.com%2Fphp%20tutorials%23iterator%20loops"

encodeURI()

This method is not like encodeURIComponent() , it will encode a URL string but not the parameters or hash.

This method will not encode following characters ~!@#$&*()=:/,;?+' and it will not encode pure URI characters.

encodeURI("https://arjunphp.com/php tutorials#iterator loops");
output : "https://arjunphp.com/php%20tutorials#iterator%20loops"
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments