Reading Repeated Parameters with URLSearchParams.getAll()
get() returns only the first value for a repeated query key, while getAll() returns every value in order as an array. getAll() also returns an empty array for a missing key, where get() returns null.
The short answer
URLSearchParams.get(name) returns the value of the first name-value pair whose name matches, or null if no pair matches. URLSearchParams.getAll(name) returns an array of every matching value in list order, or an empty array if none match. A query string is an ordered list that may contain duplicate names, so get() is lossy by design: it answers "what is the first value?" rather than "what values are there?"
A minimal example
const params = new URLSearchParams("?tag=javascript&tag=web&tag=api");
params.get("tag"); // "javascript"
params.getAll("tag"); // ["javascript", "web", "api"]
params.get("missing"); // null
params.getAll("missing"); // []
for (const [name, value] of params) {
console.log(name, value); // all three pairs, duplicates included
}
Note the different empty results: get() uses null, while getAll() uses []. Code that tests truthiness should handle both.
Why get() returns only one value
The underlying data structure is a list of pairs, not a map. URLSearchParams preserves insertion order and permits the same name any number of times. get() is a convenience for the common single-value case: it returns the first match and discards the rest. That is why a URL such as ?id=1&id=2 produces "1" from get("id") and ["1", "2"] from getAll("id").
Mutating methods treat duplicates differently, which is worth remembering:
const params = new URLSearchParams("tag=js&tag=web");
params.append("tag", "api"); // tag=js&tag=web&tag=api
params.set("tag", "only"); // replaces every tag pair with one
params.toString(); // "tag=only"
params.delete("tag"); // removes all tag pairs
append() adds a pair, set() keeps only one pair for the name, and delete() removes every pair with the name. There is no method that removes only the first duplicate.
Server-side parsing differs
The browser-side API is standardized, but how a server turns repeated keys into a value varies by framework and language:
| Parser | Result for a=1&a=2 |
|---|---|
URLSearchParams.getAll("a") |
["1", "2"] |
URLSearchParams.get("a") |
"1" |
Node.js querystring.parse |
{ a: ["1", "2"] } |
Python urllib.parse.parse_qs |
{"a": ["1", "2"]} |
PHP $_GET["a"] |
"2" (last wins without []) |
If a client sends repeated keys and the server expects one, the extra values may be silently ignored. If the server returns repeated keys, use getAll() on the client. Deciding which side is authoritative prevents the classic bug where the UI reads the first value while the backend stored the last.
Edge cases
- Matching is exact and case-sensitive:
Tagandtagare different names. - Empty values are preserved.
?a=&a=1gives["", "1"]; a bare?aalso yields an empty string. getAll()returns a fresh array. Mutating it does not change theURLSearchParamsobject.- The parser decodes percent escapes and
+as a space, andtoString()re-encodes spaces as+. - Only
&separates pairs; semicolons are not separators, even though some old servers accept them. - When working with a
URL, readurl.searchParamsdirectly; it is live and changes the URL when you mutate it. Object.fromEntries(params)collapses duplicates, keeping the last value, because object keys are unique. UsegetAll()when duplicates matter, and see building arrays with Array.from() for turning the iterator into an array of pairs.
Practical checks
const tags = new URLSearchParams(location.search).getAll("tag");
const unique = [...new Set(tags)];
- Use
getAll()whenever a key is documented as repeatable, such astag,filter, orinclude. - Test with
?key=,?key, duplicate keys, and percent-encoded keys. - When generating URLs,
params.append()is safe for repeated keys;params.set()is not.
Sources
Revision history
- Published
- Updated
- — Initial publication.