URLSearchParams
Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:867
The URLSearchParams API provides read and write access to the query of a URL. The URLSearchParams class can also be used standalone with one of the
four following constructors.
The URLSearchParams class is also available on the global object.
The WHATWG URLSearchParams interface and the querystring module have
similar purpose, but the purpose of the querystring module is more
general, as it allows the customization of delimiter characters (& and =).
On the other hand, this API is designed purely for URL query strings.
const myURL = new URL('https://example.org/?abc=123');console.log(myURL.searchParams.get('abc'));// Prints 123
myURL.searchParams.append('abc', 'xyz');console.log(myURL.href);// Prints https://example.org/?abc=123&abc=xyz
myURL.searchParams.delete('abc');myURL.searchParams.set('a', 'b');console.log(myURL.href);// Prints https://example.org/?a=b
const newSearchParams = new URLSearchParams(myURL.searchParams);// The above is equivalent to// const newSearchParams = new URLSearchParams(myURL.search);
newSearchParams.append('a', 'c');console.log(myURL.href);// Prints https://example.org/?a=bconsole.log(newSearchParams.toString());// Prints a=b&a=c
// newSearchParams.toString() is implicitly calledmyURL.search = newSearchParams;console.log(myURL.href);// Prints https://example.org/?a=b&a=cnewSearchParams.delete('a');console.log(myURL.href);// Prints https://example.org/?a=b&a=cv7.5.0, v6.13.0
Implements
Section titled “Implements”Iterable<[string,string]>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new URLSearchParams(init?): URLSearchParams;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:868
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
init? | | string | URLSearchParams | Record<string, string | readonly string[]> | Iterable<[string, string], any, any> | readonly [string, string][] |
Returns
Section titled “Returns”URLSearchParams
Properties
Section titled “Properties”readonly size: number;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:972
The total number of parameter entries.
v19.8.0
Methods
Section titled “Methods”[iterator]()
Section titled “[iterator]()”iterator: URLSearchParamsIterator<[string, string]>;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:998
Returns
Section titled “Returns”URLSearchParamsIterator<[string, string]>
Implementation of
Section titled “Implementation of”append()
Section titled “append()”append(name, value): void;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:879
Append a new name-value pair to the query string.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
name | string |
value | string |
Returns
Section titled “Returns”void
delete()
Section titled “delete()”delete(name, value?): void;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:886
If value is provided, removes all name-value pairs
where name is name and value is value.
If value is not provided, removes all name-value pairs whose name is name.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
name | string |
value? | string |
Returns
Section titled “Returns”void
entries()
Section titled “entries()”entries(): URLSearchParamsIterator<[string, string]>;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:893
Returns an ES6 Iterator over each of the name-value pairs in the query.
Each item of the iterator is a JavaScript Array. The first item of the Array is the name, the second item of the Array is the value.
Alias for urlSearchParams[Symbol.iterator]().
Returns
Section titled “Returns”URLSearchParamsIterator<[string, string]>
forEach()
Section titled “forEach()”forEach<TThis>(fn, thisArg?): void;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:909
Iterates over each name-value pair in the query and invokes the given function.
const myURL = new URL('https://example.org/?a=b&c=d');myURL.searchParams.forEach((value, name, searchParams) => { console.log(name, value, myURL.searchParams === searchParams);});// Prints:// a b true// c d trueType Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
TThis | URLSearchParams |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
fn | (this, value, name, searchParams) => void | Invoked for each name-value pair in the query |
thisArg? | TThis | To be used as this value for when fn is called |
Returns
Section titled “Returns”void
get(name): string | null;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:918
Returns the value of the first name-value pair whose name is name. If there
are no such pairs, null is returned.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
name | string |
Returns
Section titled “Returns”string | null
or null if there is no name-value pair with the given name.
getAll()
Section titled “getAll()”getAll(name): string[];Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:923
Returns the values of all name-value pairs whose name is name. If there are
no such pairs, an empty array is returned.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
name | string |
Returns
Section titled “Returns”string[]
has(name, value?): boolean;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:933
Checks if the URLSearchParams object contains key-value pair(s) based on name and an optional value argument.
If value is provided, returns true when name-value pair with
same name and value exists.
If value is not provided, returns true if there is at least one name-value
pair whose name is name.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
name | string |
value? | string |
Returns
Section titled “Returns”boolean
keys()
Section titled “keys()”keys(): URLSearchParamsIterator<string>;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:947
Returns an ES6 Iterator over the names of each name-value pair.
const params = new URLSearchParams('foo=bar&foo=baz');for (const name of params.keys()) { console.log(name);}// Prints:// foo// fooReturns
Section titled “Returns”URLSearchParamsIterator<string>
set(name, value): void;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:967
Sets the value in the URLSearchParams object associated with name to value. If there are any pre-existing name-value pairs whose names are name,
set the first such pair’s value to value and remove all others. If not,
append the name-value pair to the query string.
const params = new URLSearchParams();params.append('foo', 'bar');params.append('foo', 'baz');params.append('abc', 'def');console.log(params.toString());// Prints foo=bar&foo=baz&abc=def
params.set('foo', 'def');params.set('xyz', 'opq');console.log(params.toString());// Prints foo=def&abc=def&xyz=opqParameters
Section titled “Parameters”| Parameter | Type |
|---|---|
name | string |
value | string |
Returns
Section titled “Returns”void
sort()
Section titled “sort()”sort(): void;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:988
Sort all existing name-value pairs in-place by their names. Sorting is done with a stable sorting algorithm, so relative order between name-value pairs with the same name is preserved.
This method can be used, in particular, to increase cache hits.
const params = new URLSearchParams('query[]=abc&type=search&query[]=123');params.sort();console.log(params.toString());// Prints query%5B%5D=abc&query%5B%5D=123&type=searchReturns
Section titled “Returns”void
v7.7.0, v6.13.0
toString()
Section titled “toString()”toString(): string;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:993
Returns the search parameters serialized as a string, with characters percent-encoded where necessary.
Returns
Section titled “Returns”string
values()
Section titled “values()”values(): URLSearchParamsIterator<string>;Defined in: node_modules/.pnpm/@types+node@24.8.1/node_modules/@types/node/url.d.ts:997
Returns an ES6 Iterator over the values of each name-value pair.
Returns
Section titled “Returns”URLSearchParamsIterator<string>