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);
Call
What 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.
Plus stays: a+b
From a form: hello world!
Lone percent: URIError
Half a letter: URIError
How It Works
A plus is a plus.decodeURIComponent("a+b") keeps the +.
Form text uses + for a space. Replace every + with a space first, then decode. %21 becomes !.
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.
Compatibility
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.
ES3Standard since
Google ChromeAll versions · Desktop & Mobile
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
OperaAll versions
Full support
Samsung InternetAll versions · Android
Full support
Android WebViewAll versions
Full support
Node.jsAll versions · Server runtime
Supported
DenoAll versions · JavaScript runtime
Supported
BunAll versions · JavaScript runtime
Supported
Internet ExplorerAll 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.