URLSearchParams

URLSearchParams Class The `URLSearchParams` class represents the query parameters of a URL.

Constructor

new URLSearchParams(search)

Creates a new instance of the URLSearchParams class.
Parameters:
NameTypeDescription
searchobject | stringThe search parameters to initialize the URLSearchParams.
Examples
// get data
const params = new URLSearchParams({ key1: 'value1', key2: 'value2' });
console.log(params.get('key1')); // Output: 'value1'
console.log(params.get('key3')); // Output: null
// set & forEach
const params = new URLSearchParams();
params.set('key1', 'value1');
params.set('key2', 'value2');
params.forEach((value, key) => console.log(key + ' ' + value)); // Output: 'key1 value1' then 'key2 value2'

Methods

append(name, value)

Appends a value to the specified query parameter.
Parameters:
NameTypeDescription
namestringThe name of the query parameter.
valuestringThe value to append to the query parameter.

delete(name)

Deletes the specified query parameter.
Parameters:
NameTypeDescription
namestringThe name of the query parameter to delete.

entries() → {URLSearchParamsIterator}

Returns an iterator that iterates over the entries of the query parameters.
Returns:
- An iterator for the entries of the query parameters.
Type: 
URLSearchParamsIterator

forEach(callback, thisArgopt)

Calls a function for each query parameter in the URLSearchParams.
Parameters:
NameTypeAttributesDescription
callbackfunctionThe callback function to execute for each query parameter.
thisArgobject<optional>
Optional. The value to use as `this` when executing the callback function.

get(name) → {string|null}

Gets the value of the specified query parameter.
Parameters:
NameTypeDescription
namestringThe name of the query parameter.
Returns:
- The value of the query parameter, or null if not found.
Type: 
string | null

has(name) → {boolean}

Checks if the specified query parameter exists.
Parameters:
NameTypeDescription
namestringThe name of the query parameter.
Returns:
- True if the query parameter exists, false otherwise.
Type: 
boolean

keys() → {URLSearchParamsIterator}

Returns an iterator that iterates over the keys of the query parameters.
Returns:
- An iterator for the keys of the query parameters.
Type: 
URLSearchParamsIterator

set(name, value)

Sets the value of the specified query parameter.
Parameters:
NameTypeDescription
namestringThe name of the query parameter.
valuestringThe value of the query parameter.

sort()

Sorts the query parameters.

toString() → {string}

Converts the URLSearchParams to a string representation.
Returns:
- The string representation of the URLSearchParams.
Type: 
string

values() → {URLSearchParamsIterator}

Returns an iterator that iterates over the values of the query parameters.
Returns:
- An iterator for the values of the query parameters.
Type: 
URLSearchParamsIterator