Get the value of URL `Parameters`
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Getting a URL parameter value is easy if you use the platform parser instead of splitting strings by hand. In modern JavaScript, URL and URLSearchParams handle decoding, repeated keys, and missing values correctly. The real engineering decision is not how to split ?a=1&b=2, but how to parse once, validate, and keep URL state consistent across the application.
Use URLSearchParams for Browser Code
If you are working in a browser, the simplest approach is URLSearchParams.
get returns the first value for a key or null if the key does not exist. That is already better than hand-written parsing logic because it handles decoding and separators correctly.
If you have a full URL string rather than window.location.search, use URL first:
Repeated Keys Need getAll
Some URLs repeat the same key multiple times, especially for filters and tags.
get returns only the first matching value. If your routing or filtering logic expects multiple values, getAll is the correct API.
This matters more than people think. A bug caused by silently dropping repeated filters can be hard to notice because the URL still looks valid.
Parse and Convert at the Boundary
Query parameters are strings. Even if they represent page numbers, booleans, or limits, they arrive as text. Convert them at the boundary instead of passing raw strings deep into the application.
This pattern keeps the rest of the code working with typed values instead of repeating conversion logic everywhere.
Update URL Parameters Safely Too
Reading parameters is only half the job. If the application also writes URLs, use the same API for construction.
This prevents broken encoding and keeps query generation consistent with parsing.
Frameworks Still Benefit from One Parsing Helper
Even when using a router library, centralizing URL parsing is a good idea. Framework helpers are useful, but the application still needs one place where defaults, bounds, and conversions are defined.
The principle is the same whether you are in React, Vue, Svelte, or plain browser code: parse once, validate early, and update through one path.
Common Pitfalls
- Splitting query strings manually and breaking on encoded characters or edge cases.
- Using
getwhen the URL format actually allows repeated keys and needsgetAll. - Passing raw string parameters deep into business logic without conversion or validation.
- Building query strings by concatenating strings instead of using
URLSearchParams. - Treating the URL as a free-form text field instead of a typed application boundary.
Summary
- Use
URLSearchParamsto read query parameters in modern JavaScript. - Use
getfor one value andgetAllfor repeated keys. - Convert parameter strings into typed values as soon as you parse them.
- Use the same API to write parameters safely.
- Centralized parsing keeps route logic predictable and easier to maintain.

