URLSearchParams: size property
Note: This feature is available in Web Workers.
The size
read-only property of the URLSearchParams
interface indicates the total number of search parameter entries.
Value
A number indicating the total number of search parameter entries in the URLSearchParams
object.
Examples
Getting the amount of search parameter entries
You can get the total number of search parameter entries like so:
js
const searchParams = new URLSearchParams("c=4&a=2&b=3&a=1");
searchParams.size; // 4
Note how the a
parameter is given twice, but size
returns the number of all given entries (4) and not 3. To get the amount of unique keys, you can use a Set
, for example:
js
[...new Set(searchParams.keys())].length; // 3
Checking if search parameters exist
The size
property is useful for checking whether there are any search parameters at all:
js
const url = new URL("https://example.com?foo=1&bar=2");
if (url.searchParams.size) {
console.log("URL has search parameters!");
}
Specifications
Specification |
---|
URL # dom-urlsearchparams-size |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
size |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
- Has more compatibility info.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.