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 (`&#x26;` 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&#x26;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=b
    console.log(newSearchParams.toString());
    // Prints a=b&#x26;a=c

    // newSearchParams.toString() is implicitly called
    myURL.search = newSearchParams;
    console.log(myURL.href);
    // Prints https://example.org/?a=b&#x26;a=c
    newSearchParams.delete('a');
    console.log(myURL.href);
    // Prints https://example.org/?a=b&#x26;a=c

since

  v7.5.0, v6.13.0

### Hierarchy

- URLSearchParams

### Implements

- Iterable<[string, string]>

## Index

### Constructors

- constructor

### Methods

- [iterator]
- append
- delete
- entries
- forEach
- get
- getAll
- has
- keys
- set
- sort
- toString
- values

## Constructors

### constructor

- new URLSearchParams(init?: string | readonly [string, string][] | URLSearchParams | Record<string, string | readonly string[]> | Iterable<[string, string]>): URLSearchParams

- #### Parameters

  - ##### Optional init: string | readonly [string, string][] | URLSearchParams | Record<string, string | readonly string[]> | Iterable<[string, string]>

  #### Returns URLSearchParams

## Methods

### [iterator]

- [iterator](): IterableIterator<[string, string]>

- Implementation of Iterable.[iterator]

  #### Returns IterableIterator<[string, string]>

### append

- append(name: string, value: string): void

- Append a new name-value pair to the query string.

  #### Parameters

  - ##### name: string

  - ##### value: string

  #### Returns void

### delete

- delete(name: string): void

- Remove all name-value pairs whose name is `name`.

  #### Parameters

  - ##### name: string

  #### Returns void

### entries

- entries(): IterableIterator<[string, string]>

- 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[@@iterator]()`.

  #### Returns IterableIterator<[string, string]>

### forEach

- forEach<TThis>(callback: (this: TThis, value: string, name: string, searchParams: URLSearchParams) => void, thisArg?: TThis): void

- Iterates over each name-value pair in the query and invokes the given function.

      const myURL = new URL('https://example.org/?a=b&#x26;c=d');
      myURL.searchParams.forEach((value, name, searchParams) => {
        console.log(name, value, myURL.searchParams === searchParams);
      });
      // Prints:
      //   a b true
      //   c d true

  #### Type parameters

  - #### TThis = URLSearchParams

  #### Parameters

  - ##### callback: (this: TThis, value: string, name: string, searchParams: URLSearchParams) => void

    - - (this: TThis, value: string, name: string, searchParams: URLSearchParams): void

      - #### Parameters

        - ##### this: TThis

        - ##### value: string

        - ##### name: string

        - ##### searchParams: URLSearchParams

        #### Returns void

  - ##### Optional thisArg: TThis

    To be used as `this` value for when `fn` is called

  #### Returns void

### get

- get(name: string): string

- Returns the value of the first name-value pair whose name is `name`. If there are no such pairs, `null` is returned.

  #### Parameters

  - ##### name: string

  #### Returns string

  or `null` if there is no name-value pair with the given `name`.

### getAll

- getAll(name: string): string[]

- Returns the values of all name-value pairs whose name is `name`. If there are no such pairs, an empty array is returned.

  #### Parameters

  - ##### name: string

  #### Returns string[]

### has

- has(name: string): boolean

- Returns `true` if there is at least one name-value pair whose name is `name`.

  #### Parameters

  - ##### name: string

  #### Returns boolean

### keys

- keys(): IterableIterator<string>

- Returns an ES6 `Iterator` over the names of each name-value pair.

      const params = new URLSearchParams('foo=bar&#x26;foo=baz');
      for (const name of params.keys()) {
        console.log(name);
      }
      // Prints:
      //   foo
      //   foo

  #### Returns IterableIterator<string>

### set

- set(name: string, value: string): void

- 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&#x26;foo=baz&#x26;abc=def

      params.set('foo', 'def');
      params.set('xyz', 'opq');
      console.log(params.toString());
      // Prints foo=def&#x26;abc=def&#x26;xyz=opq

  #### Parameters

  - ##### name: string

  - ##### value: string

  #### Returns void

### sort

- sort(): void

- 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&#x26;type=search&#x26;query[]=123');
      params.sort();
      console.log(params.toString());
      // Prints query%5B%5D=abc&#x26;query%5B%5D=123&#x26;type=search

  since

    v7.7.0, v6.13.0

  #### Returns void

### toString

- toString(): string

- Returns the search parameters serialized as a string, with characters percent-encoded where necessary.

  #### Returns string

### values

- values(): IterableIterator<string>

- Returns an ES6 `Iterator` over the values of each name-value pair.

  #### Returns IterableIterator<string>

Generated using TypeDoc