decodeURIComponent()
The global decodeURIComponent() function reverses percent-encoding, turning each %XX escape sequence in a string back into the character it stands for. It decodes every escape, including delimiters like %26 for & and %2F for /, so it suits one part of a URI, such as a query parameter value or a path segment:
> decodeURIComponent("Jack%20%26%20Jill")
'Jack & Jill'
> decodeURIComponent("caf%C3%A9%2Fmenu%3F")
'café/menu?'
decodeURIComponent() is an ECMAScript built-in, so it works the same in browsers and server runtimes. It’s the inverse of encodeURIComponent(). The related decodeURI() function uses the same algorithm but keeps escapes for URI delimiters such as %2F and %3F, because it expects a whole URI.
Syntax
The decodeURIComponent() function takes one argument:
decodeURIComponent(<encodedURI>)
| Parameter | Description |
|---|---|
encodedURI |
The percent-encoded text to decode. A non-string value is converted to a string first, so a missing argument becomes "undefined" |
The function isn’t a constructor, so calling it with new throws a TypeError.
Return Value
The result is a new string. Characters outside escape sequences are copied unchanged. Each run of one to four %XX sequences that forms the UTF-8 encoding of one character becomes that character. Hexadecimal digits can be uppercase or lowercase.
The call throws a URIError instead of returning when a % isn’t followed by two hexadecimal digits. It also throws when the escaped bytes aren’t valid UTF-8, such as a multibyte sequence cut short.
Examples
A file server receives the path /files/r%C3%A9sum%C3%A9%2Fv2.pdf and needs the original file name. Splitting the path before decoding keeps the encoded / inside the name from creating an extra segment:
> const path = "/files/r%C3%A9sum%C3%A9%2Fv2.pdf";
undefined
> path.split("/")
[ '', 'files', 'r%C3%A9sum%C3%A9%2Fv2.pdf' ]
> decodeURIComponent(path.split("/")[2])
'résumé/v2.pdf'
A site stores a display name in a cookie, encoded so that spaces and accented letters survive the Cookie header. Reading it back decodes the value after splitting off the name:
> const cookie = "theme=dark; user=Ana%20Mar%C3%ADa";
undefined
> decodeURIComponent(cookie.split("; ")[1].split("=")[1])
'Ana María'
Common Gotchas
A + stays a plus sign. HTML form encoding uses + for spaces, so a query string from a submitted form needs URLSearchParams instead:
> decodeURIComponent("rock+%26+roll")
'rock+&+roll'
> new URLSearchParams("q=rock+%26+roll").get("q")
'rock & roll'
A stray % that doesn’t start a valid escape makes the call throw, which happens with hand-typed links such as ?promo=50% off. A try...catch block lets code fall back to the raw text:
> decodeURIComponent("50% off")
Uncaught URIError: URI malformed
at decodeURIComponent (<anonymous>)
> function safeDecode(text) {
… try {
… return decodeURIComponent(text);
… } catch {
… return text;
… }
… }
undefined
> safeDecode("50% off")
'50% off'
Decoding a value twice can change it or throw. The first call turns %25 into a literal %, and the second call then reads that % as the start of an escape:
> decodeURIComponent("100%25")
'100%'
> decodeURIComponent(decodeURIComponent("100%25"))
Uncaught URIError: URI malformed
at decodeURIComponent (<anonymous>)
Related Resources
Course
JavaScript Strings for Pythonistas
Port a payments team's branch-name helper to JavaScript, and learn where Python's string habits, from slicing to split() and join(), run without an error and give the wrong text.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 14, 2026