JavaScript decodeURIComponent() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Global function

Definition

decodeURIComponent() does the opposite of encodeURIComponent(). It turns every percent code back into its character, so "rock%20%26%20roll" becomes "rock & roll".

Use it to read one value from a URL, such as a query parameter or a path part. Unlike decodeURI(), it also decodes the codes for / ? # & = +.

Remember
decodeURIComponent(encodedValue);

encodedValue -> one value with %XX codes
returns      -> a new string with every code turned back into a character

A plus sign stays a plus sign (it is NOT turned into a space).
Malformed codes (like "%") throw a URIError.

Syntax

Call decodeURIComponent() by name and give it the encoded value as text. It returns the decoded text and does not change the original.

JavaScript
decodeURIComponent(encodedValue);
CallWhat it does
decodeURIComponent("rock%20%26%20roll")Space and ampersand are decoded. Result: "rock & roll".
decodeURIComponent("a%2Fb%3Fc")Slash and question mark are decoded too. Result: "a/b?c".
decodeURIComponent("caf%C3%A9")The two UTF-8 bytes become one letter, e with an accent.
decodeURIComponent("%")A lone % is not a valid code. It throws a URIError.

Built into JavaScript (ES3). It is a global function, so you call it by name with no import. See also: decodeURI(), encodeURIComponent() and encodeURI().

Library declarations
JavaScript
// Global function (ES3)
decodeURIComponent(encodedURIComponent)

// returns a string
// throws URIError for a malformed % sequence

Parameters

ParameterDescription
encodedValueThe text to decode, normally one value that was encoded with encodeURIComponent(). A value that is not text is converted to text first.

Return Value

Return ValueDescription
A stringA copy of the text where each %XX code (or group of codes for one character) is replaced by the character. A + is not changed.
URIError (thrown)A % is not followed by two hexadecimal digits, or the bytes do not form a valid UTF-8 character.

JavaScript Examples

Example 1 — Decode a Query Value

Turn an encoded search phrase back into the text the user typed.

JavaScript
const encoded = "rock%20%26%20roll%20%2F%20100%25%20fun%3F";

console.log(decodeURIComponent(encoded));
console.log("Accent: " + (decodeURIComponent("caf%C3%A9") === "caf\u00e9"));

const original = "50% off & more / today?";
console.log("Round trip: " + (decodeURIComponent(encodeURIComponent(original)) === original));
Try It Yourself

How It Works

  1. Every code is decoded. %20, %26, %2F, %25 and %3F all go back to their characters.
  2. UTF-8 bytes join up. %C3%A9 is one letter, e with an accent. The comparison with "caf\u00e9" is true.
  3. Round trip. Encoding and then decoding with the component functions gives back the original text, even with &, % and ? in it.

Example 2 — Read the Values of a Query String

Split the query string into pairs first, and decode each key and value on its own.

JavaScript
const query = "name=Mia%20Lee&city=S%C3%A3o%20Paulo&note=50%25%20off%20%26%20more";
const result = {};

query.split("&").forEach(function (pair) {
  const parts = pair.split("=");
  result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
});

console.log("name: " + result.name);
console.log("city is Sao Paulo: " + (result.city === "S\u00e3o Paulo"));
console.log("note: " + result.note);
Try It Yourself

How It Works

  1. Split first. query.split("&") gives three pairs. The %26 inside the note is still encoded, so it does not split anything.
  2. Split each pair. pair.split("=") gives the key and the value.
  3. Decode each side. Both the key and the value go through decodeURIComponent() and are stored in the result object.
  4. Use the values. result.note now contains a real & and a real %, because decoding happened last.

Example 3 — Handle Plus Signs and Bad Codes

A plus sign is not a space for this function, and a broken code throws an error.

JavaScript
console.log("Plus stays: " + decodeURIComponent("a+b"));

const fromForm = "hello+world%21";
console.log("From a form: " + decodeURIComponent(fromForm.replace(/\+/g, " ")));

try {
  decodeURIComponent("%");
} catch (err) {
  console.log("Lone percent: " + err.name);
}

try {
  decodeURIComponent("%C3");
} catch (err) {
  console.log("Half a letter: " + err.name);
}
Try It Yourself

How It Works

  1. A plus is a plus. decodeURIComponent("a+b") keeps the +.
  2. Form text uses + for a space. Replace every + with a space first, then decode. %21 becomes !.
  3. Errors are catchable. A lone % and a code that starts a longer letter but stops early (%C3 needs a second byte) both throw a URIError. Catch it with try...catch.

Browser & Runtime Support

decodeURIComponent() arrived with ES3 and has worked in every browser for a very long time. Every Node.js version has it too.

✓ ES3 · Works everywhere

decodeURIComponent()

Supported in all browsers, including old ones, and in Node.js, Deno and Bun.

ES3 Standard since
Google Chrome All versions · Desktop & Mobile
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Opera All versions
Full support
Samsung Internet All versions · Android
Full support
Android WebView All versions
Full support
Node.js All versions · Server runtime
Supported
Deno All versions · JavaScript runtime
Supported
Bun All versions · JavaScript runtime
Supported
Internet Explorer All versions
Full support
decodeURIComponent() Excellent

Bottom line: Safe to use anywhere. For query strings in new code, URLSearchParams reads and decodes the values for you, and also turns + into a space.

Frequently Asked Questions

It turns every percent code in the text back into its character. For example decodeURIComponent("rock%20%26%20roll") returns "rock & roll". It is used to read one value from a URL, such as a query parameter.
decodeURIComponent() decodes every code, including the codes for / ? # & = and +. decodeURI() keeps those codes as they are, because it is meant for a whole URL. Use decodeURIComponent() for single values.
No. HTML forms write a space as + in a query string, but this function leaves + alone. Replace + with a space before you decode: decodeURIComponent(text.replace(/\+/g, " ")). URLSearchParams does this for you.
The text has a % that is not followed by two hexadecimal digits, or a group of codes that is not a valid UTF-8 character, such as %C3 on its own. Fix the text, or wrap the call in try...catch and use the original text when decoding fails.
Split the query string on & first, split each pair on =, and then run decodeURIComponent() on the key and on the value. In new code you can also use new URLSearchParams(window.location.search).get("name"), which does all of this.

Next: encodeURI()

Learn how to encode a complete web address so spaces and special letters are safe.

encodeURI() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful