JavaScript

Difference between encodeURIComponent() and encodeURI() in JavaScript

As you may know, in javascript, these functions allow encoding a URI(Uniform Resource Identifier) by replacing each instance of some characters with one, two, three, or four escape sequences representing the UTF-8 encoding of the character.
 

Difference between encodeURIComponent() and encodeURI()

There are no big differences, the only difference is that the encodeURI() function encodes special characters, except: , /? : @ & = + $ # while the encodeURIComponent() function encodes special characters and in addition the above characters(, /? : @ & = + $ #).
 

 

Example :
var url = 'https://stackhowto.com/search?q='
var req = 'St@ckHowTo'

// encode only the string 'req' with encodeURIComponent
var url1 = url + encodeURIComponent(req)
console.log(url1)  //Output: https://stackhowto.com/search?q=St%40ckHowTo

// encode only the string 'req' with encodeURI
var url2 = url + encodeURI(req)
console.log(url2)  //Output: https://stackhowto.com/search?q=St@ckHowTo
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *