You can use the decodeURIComponent() and decodeURI() functions in JavaScript to decode a URL.

decodeURIComponent() function is used to decode a URL encoded using the encodeURIComponent() function. It decodes the encoded string into the original string.

For Example:

let encoded = "https%3A%2F%2Fexample.com%2Fsearch%3Fq%3Dhello%20world";
let decoded = decodeURIComponent(encoded);
console.log(decoded); // "https://example.com/search?q=hello world"

decodeURI() function is used to decode a URL encoded using the encodeURI() function. It also decodes the encoded string into the original string.

For Example:

let encoded = "https://example.com/search?q=hello%20world";
let decoded = decodeURI(encoded);
console.log(decoded); // "https://example.com/search?q=hello world"

It’s important to note that if you try to decode an encoded string that was not encoded using the corresponding function, it may not work as expected.

Also, if the encoded string contains any malformed percent-encoded sequences, you will get a URIError.

Also Read:

Categorized in: