Read repeated query parameters URLSearchParams getAll
Learn how to read repeated query parameters safely without losing data by using the URLSearchParams getAll method in JavaScript.
Table of Contents7 sections

When an application URL contains the same parameter key multiple times, standard property lookups or single-value getters keep only the final instance and silently discard the rest of the inputs.
Quick solution:
Use the getAll method to retrieve every matching value as an array.
const params = new URLSearchParams("?tag=css&tag=javascript&tag=css");
const tags = params.getAll("tag");
console.log(tags); // ["css", "javascript", "css"]
Handling uniform query strings without data loss requires knowing how the native interface manages repeated entries. Developers often run into trouble when parsing URLs that allow multi-select filters, comma-separated alternatives, or duplicated category flags. When you need to read repeated query parameters URLSearchParams getAll is the standard browser API designed specifically to return every associated string without overwriting earlier occurrences.
Understanding the getAll Behavior
The MDN URLSearchParams.getAll reference explains that calling this method with a specific key returns an array of all string values associated with that exact parameter name in the order they appear in the query string. If the query string does not contain the requested key, the method returns an empty array instead of throwing an error or returning null. This design prevents common runtime crashes associated with missing properties. However, you must remember that all collected elements are raw strings. If your application expects numbers, booleans, or parsed objects, you must transform those items after extraction.
Collecting Raw Values Versus Interpreting Filters
Collecting raw query strings is fundamentally different from validating or interpreting application filters. The URLSearchParams interface only parses the structural query string from a URL and does not know whether a specific tag is allowed, deprecated, or duplicated. For instance, if a user appends duplicate tags like ?tag=css&tag=css, the getAll method preserves both entries. Business logic must handle deduplication or validation separately. If you are also dealing with other complex request data payloads, you might encounter scenarios similar to how developers handle cases when they fetch error response bodies bound before parsing JSON safely in asynchronous API clients.
Decision Table for Multi-Value Query Handling
| Option | When to Use It | Trade-off or Failure Mode | Recommendation |
|---|---|---|---|
URLSearchParams.get |
When only the single last parameter value matters | Silently drops all previous duplicate keys | Avoid for multi-select inputs |
URLSearchParams.getAll |
When every repeated parameter instance is required | Returns raw strings and does not deduplicate | Use for multi-value filters |
| Custom Split Parser | When parameters use comma-separated formats | Prone to escaping bugs and edge-case errors | Prefer native methods over custom parsers |
Handling Absent Keys and Empty Results
When a query parameter is completely absent from the search string, getAll yields an empty array. Your code should check for this empty state before attempting array transformations or rendering filter badges in the user interface. Relying on an empty array makes conditional rendering straightforward because array length checks evaluate safely without throwing undefined reference errors.
const params = new URLSearchParams("?category=news");
const tags = params.getAll("tag");
if (tags.length === 0) {
console.log("No tags provided in query string.");
}
Application Validation and State Rules
Isolating string collection from domain validation protects your routing layer from unexpected input failures. Because query parameters arrive from external user navigation, values can be malformed, duplicated, or overly verbose. Developers must iterate over the resulting array to perform sanitization checks, enforce maximum selection limits, or reject unknown filter keys before applying them to database queries or state stores. Similar rigor applies when developers debug http 422 api validation errors safely during backend ingestion to prevent bad query parameters from breaking downstream services.
Verification Checklist
- Confirm that passing a repeated key such as
?tag=css&tag=javascriptreturns an array containing both values in exact order. - Verify that querying a non-existent parameter name returns an empty array
[]rather than null or undefined. - Check that string extraction remains isolated from downstream business rules and type validation logic.
Continue Exploring
You Might Also Like

Architecting a 3-Column Editorial Grid
Learn how to build a high-density 3-column editorial catalog with zero-bleed sticky navigation, accessible stretched-link card patterns, and robust mobile flex containment.

Decode Split UTF-8 Chunks with TextDecoder Streaming
Learn how to correctly decode split UTF-8 chunks using JavaScript TextDecoder streaming options, handling byte boundaries and malformed input safely.

Promise allSettled Independent Task Failures
Learn how to handle independent JavaScript task failures safely using Promise.allSettled without losing partial outcomes.