URLSearchParams: has() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

Note: This feature is available in Web Workers.

The has() method of the URLSearchParams interface returns a boolean value that indicates whether the specified parameter is in the search parameters.

A parameter name and optional value are used to match parameters. If only a parameter name is specified, then the method will return true if any parameters in the query string match the name, and false otherwise. If both a parameter name and value are specified, then the method will return true if a parameter matches both the name and value.

Syntax

js
has(name)
has(name, value)

Parameters

name

The name of the parameter to match.

value

The value of the parameter, along with the given name, to match.

Return value

A boolean value.

Examples

Check for parameter with specified name

This example shows how to check if the query string has any parameters with a particular name.

js
const url = new URL("https://example.com?foo=1&bar=2&foo=3");
const params = new URLSearchParams(url.search);

// has() returns true if the parameter is in the query string
console.log(`bar?:\t${params.has("bar")}`);
console.log(`bark?:\t${params.has("bark")}`);
console.log(`foo?:\t${params.has("foo")}`);

The log below shows whether the parameters bar, bark, and foo, are present in the query string.

bar?:  true
bark?: false
foo?:  true

Check for parameter with specified name and value

This example shows how to check whether the query string has a parameter that matches both a particular name and value.

js
const url = new URL("https://example.com?foo=1&bar=2&foo=3");
const params = new URLSearchParams(url.search);

// has() returns true if a parameter with the matching name and value is in the query string
console.log(`bar=1?:\t${params.has("bar", "1")}`);
console.log(`bar=2?:\t${params.has("bar", "2")}`);
console.log(`foo=4?:\t${params.has("foo", "4")}`);

Only the second value above should be true, as only the parameter name bar with value 2 is matched.

bar=1?: false
bar=2?: true
foo=4?: false

If your browser does not support the value option the method will match on the name, and all the results should be true.

Specifications

Specification
URL
# dom-urlsearchparams-has

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
has
Chrome – Full support
Chrome 49 (Release date: 2016-03-02)
footnote Full support
Edge – Full support
Edge 17 (Release date: 2018-04-30)
footnote Full support
Firefox – Full support
Firefox 29 (Release date: 2014-04-29)
footnote Full support
Opera – Full support
Opera 36 (Release date: 2016-03-15)
footnote Full support
Safari – Full support
Safari 10.1 (Release date: 2017-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 49 (Release date: 2016-03-09)
footnote Full support
Firefox for Android – Full support
Firefox for Android 29 (Release date: 2014-04-29)
footnote Full support
Opera Android – Full support
Opera Android 36 (Release date: 2016-03-31)
footnote Full support
Safari on iOS – Full support
Safari on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – Full support
WebView Android 49 (Release date: 2016-03-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 6.13 (Release date: 2018-02-13)
footnote Full support
value parameter
Chrome – Full support
Chrome 117 (Release date: 2023-09-12)
footnote Full support
Edge – Full support
Edge 117 (Release date: 2023-09-15)
footnote Full support
Firefox – Full support
Firefox 115 (Release date: 2023-07-04)
footnote Full support
Opera – Full support
Opera 103 (Release date: 2023-10-03)
footnote Full support
Safari – Full support
Safari 17 (Release date: 2023-09-18)
footnote Full support
Chrome Android – Full support
Chrome Android 117 (Release date: 2023-09-12)
footnote Full support
Firefox for Android – Full support
Firefox for Android 115 (Release date: 2023-07-04)
footnote Full support
Opera Android – Full support
Opera Android 78 (Release date: 2023-10-23)
footnote Full support
Safari on iOS – Full support
Safari on iOS 17 (Release date: 2023-09-18)
footnote Full support
Samsung Internet – Full support
Samsung Internet 24 (Release date: 2024-01-25)
footnote Full support
WebView Android – Full support
WebView Android 117 (Release date: 2023-09-12)
footnote Full support
WebView on iOS – Full support
WebView on iOS 17 (Release date: 2023-09-18)
footnote Full support
Bun – Full support
Bun 1.0.31 (Release date: 2024-03-15)
footnote Full support
Deno – Full support
Deno 1.35 (Release date: 2023-07-05)
footnote Full support
Node.js – No support
Node.js 18.18 – 18.20 (Release date: 2023-09-18)
footnote Removed in 19 and later
Node.js – Full support
Node.js 20.2 (Release date: 2023-05-16)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Has more compatibility info.

See also