JavaScript What is encodeURI() Function and How it Works
Hello devs, today we'll learn about the encodeURI() function. How its works or when we need them.
encodeURI() is a built-in JavaScript function that takes a string as input and returns a new string with all characters that are not allowed in a URI (Uniform Resource Identifier) encoded.
The function replaces each instance of certain characters with its corresponding URI encoding. Characters that are not ASCII letters, digits, or one of the following characters: ; , / ? : @ & = $ - _ . ! ~ * ' ( ) #, are replaced with a percent sign ("%") followed by two hexadecimal digits that represent the ASCII code of the character.
Here's an example:
let uri = 'shouts.dev?name and author=tanvir&age=25#link'
let coded = encodeURI(uri);
console.log(coded);
Which will produce the below result:
It can be helpful for generating a valid URI for specially non-ASCII character which is not supported by other systems.
That's it for today. I hope you've enjoyed this tutorial. Thanks for reading. ๐