Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Global Functions and Values

The decodeURI() function

Reference for decodeURI(): decoding a whole URL while leaving reserved characters escaped, how it differs from decodeURIComponent(), and URIError cases.

decodeURI() reverses encodeURI(). It replaces percent-escapes with the characters they stand for, with one important exception: escapes that stand for reserved URL characters (; / ? : @ & = + $ , #) are left as they are. This keeps a decoded URL from changing its meaning.

For example %2F stays %2F, because turning it into / would add a new path segment.

JavaScript
decodeURI("https://example.com/my%20page?q=caf%C3%A9");
// "https://example.com/my page?q=cafรฉ"

Try it

JS Decoding a URL

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS decodeURI versus decodeURIComponent

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
decodeURI(encodedURI)
Name Description
encodedURI The URL text to decode. Non-strings are converted to strings first.

Return value: a new string in which percent-escapes are decoded, except those that would produce reserved URL characters.

Exceptions: a URIError if a % is not followed by two hex digits, or if the bytes are not valid UTF-8.

Usage notes

  • Reserved escapes survive. Escapes for ; / ? : @ & = + $ , # stay in their %XX form. Use decodeURIComponent() if you want them decoded.
  • Use it on whole URLs, for example to display a readable address. For a single query value, decodeURIComponent() is the right tool.
  • Case of the hex digits is irrelevant when decoding: %c3%a9 and %C3%A9 both give รฉ.
  • Malformed text throws; wrap input from outside your control in try...catch.
  • Do not use decoded URLs for requests. A readable address with spaces or non-ASCII characters is for display; keep the encoded form for navigation and fetching.
  • For query values, new URL(text).searchParams.get("name") returns the decoded value directly.

Browser support

decodeURI() is available in every browser and runtime.