URLSearchParams: Safely Build and Parse URL Queries

Think of `URLSearchParams` as a structured object for a URL's query string, saving you from messy string manipulation. Use it to read incoming parameters or build a query for a fetch request. The footgun: `get()` only returns the first value for a key.
`URLSearchParams` is a dedicated API for a URL's query string, acting like a specialized `Map` to save you from error-prone string splitting and manual encoding. It's essential for reading parameters from the current URL or building a query for a `fetch` request. The main footgun is using `get()` when a key can appear multiple times (e.g., `?id=1&id=2`). `get('id')` only returns '1'; you must use `getAll('id')` to get the array `['1', '2']`.
Read the original → developer.mozilla.org
- #web api
- #typescript
- #url
- #http
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.