How to decode a URL using JavaScript

In an earlier article, we looked at different ways to encode a URL in JavaScript. In this article, you'll learn how to decode an encoded URL in JavaScript.

URL decoding is the opposite of the encoding process. It converts the encoded URL strings and query parameters back to their original formats. Mostly encoded query string parameters are automatically decoded by the underlying framework you're using, like Express or Spring Boot. However, in standalone applications, you have to manually decode query strings.

Let us look at the JavaScript native functions that can be used for this purpose.

decodeURI() method

The decodeURI() function is used to decode a full URL in JavaScript. It performs the reverse operation of encodeURI(). Here is an example:

const encodedUrl = 'http://example.com/!leearn%20javascript$/'

// decode complete URL
const url = decodeURI(encodedUrl)

// print decoded URL
console.log(url)

// output: http://example.com/!leearn javascript$/

decodeURIComponent() method

The decodeURIComponent() function is used to decode URL components encoding by encodeURIComponent() in JavaScript. It uses the UTF-8 encoding scheme to perform the decoding operation.

The decodeURIComponent() method is suitable for decoding query string parameters and path segments instead of a complete URL.

Here is an example:

const query = 'Danke Schön'

// perofrm encode/decode
const encodedStr = encodeURIComponent(query)
const decodedStr = decodeURIComponent(encodedStr)

// print values
console.log(`Encoded Query: ${encodedStr}`)
console.log(`Decoded Query: ${decodedStr}`)

// Output
// Encoded Query: Danke%20Sch%C3%B6n
// Decoded Query: Danke Schön

Read Next: Base64 encoding and decoding in JavaScript

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.