URLs, standing for Uniform Resource Locators, are a way to identify a resource on the web.
You use URLs to visit websites, point to specific files, and to give names to IP addresses.
The web supports sending data via URLs by encoding it in the URL.
The main point of encoding URLs is to make sure that the characters in the URL are safe to use.
This means that any characters that are unsafe must be escaped.
The first method,
encodeURI, takes an existing URL and encodes it safely.
encodeURI method encoded the space in the URL as
%20, which made the entire URL safe.
If you want to encode query parameters, you can use the
Either way, the
encodeURIComponent methods both help accomplish the same task, turning unsafe characters into safe ones, thereby encoding the URL.
Simply use either the
encodeURIComponent methods to encode a string safely.
Thanks for reading and happy coding!
If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on Twitter! You can also join the conversation over at our official Discord!
Leave us a message!