A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript-encodeuri-decodeuri-and-its-components-functions/ below:

JavaScript encodeURI(), decodeURI() and its components Functions

JavaScript encodeURI(), decodeURI() and its components Functions

Last Updated : 11 Jul, 2025

The encodeURI() and decodeURI() functions in JavaScript are used to handle URI (Uniform Resource Identifier) encoding and decoding. They ensure that URIs are properly formatted for web usage, converting characters that may cause issues into a valid, encoded format.

1. encodeURI() Function

The encodeURI() function encodes a URI by replacing certain characters with their UTF-8 escape sequences. It preserves characters that are valid in a URI, like :, /, ?, and #.

JavaScript
let uri = "https://example.com/query?name=Amit Kumar&age=25";
let encoded = encodeURI(uri);
console.log(encoded);

Output
https://example.com/query?name=Amit%20Kumar&age=25

encodeURI() converts the space in "Amit Kumar" into %20 but leaves the ?, =, and & characters intact, as they are part of the URI syntax.

2. decodeURI() Function

The decodeURI() function decodes an encoded URI by replacing escape sequences with their original characters.

JavaScript
let uri = "https://example.com/query?name=Amit%20Kumar&age=25";
let decoded = decodeURI(uri);
console.log(decoded);

Output
https://example.com/query?name=Amit Kumar&age=25

decodeURI() converts the %20 back into a space, restoring the original URI.

encodeURIComponent() and decodeURIComponent()

In addition to encodeURI() and decodeURI(), JavaScript provides the encodeURIComponent() and decodeURIComponent() functions, which operate on individual components of a URI.

1. encodeURIComponent()

Encodes a URI component (such as query string parameters or path segments) and encodes characters like &, =, ?, and others.

JavaScript
let name = "Amit Kumar";
let encoded = encodeURIComponent(name);
console.log(encoded);

encodeURIComponent() encodes the space in "Amit Kumar" into %20, as it treats each component as part of a larger URI.

2. decodeURIComponent()

Decodes a URI component back into its original format.

JavaScript
let encoded = encodeURIComponent("Amit Kumar");
let decoded = decodeURIComponent(encoded);
console.log(decoded);

decodeURIComponent() decodes %20 back into a space, restoring the original string.

Advantages Key Differences: Function Purpose Encodes/Decodes encodeURI()

Encodes a full URI, leaving URI delimiters (:, /, ?, &) intact

Encodes non-URI characters decodeURI() Decodes a full URI, reversing percent-encoding for non-URI characters Decodes percent-encoded URI encodeURIComponent() Encodes individual URI components (query parameters, path segments) Encodes all characters decodeURIComponent() Decodes individual URI components back into their original form Decodes percent-encoded component

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4