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 = '!leearn%20javascript$/'

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

// print decoded URL

// output:!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.