decodeURIComponent
Decodes text that was encoded for a URL.
Available since: ES3
Syntax
decodeURIComponent(encodedStr)Parameters
| Parameter | Type | Description |
|---|---|---|
| encodedStr | string | A string with codes like %20. |
Return value
stringThe decoded text.
Examples
The reverse conversion:
console.log(decodeURIComponent('rock%20%26%20roll'));
console.log(decodeURIComponent('caf%C3%A9'));Output:
rock & roll
caféAn invalid code throws an error:
try {
decodeURIComponent('100%');
} catch (error) {
console.log(error.name);
}Output:
URIErrorNotes
This function does not turn + into a space, although forms encode spaces that way. Read form parameters with URLSearchParams instead.
See also
- encodeURIComponent — Encodes text for safely putting it into part of a URL.
- URLSearchParams — Reads and builds URL query parameters (the part after ?).
In the tutorial
- Server requests: fetch — The fetch function in JavaScript: GET and POST requests, the response object, status, response.ok, parsing JSON and handling errors.