How to Read a Query String Parameter in JavaScript with URLSearchParams.get
new URLSearchParams(queryString).get(name) gives you the value of one parameter in a query string such as ?name=Ana&city=New%20York. It decodes the value, always returns a string, and returns null when the parameter isn't there.
Syntax
const <params> = new URLSearchParams(<query string>);
<params>.get(<name>)
| Part | What it does |
|---|---|
<query string> | The part of a web address from the ? on, such as '?name=Ana&seats=2'. The ? at the start is optional. |
<params> | A name for the new URLSearchParams object, which holds every name and value of the query string. |
<name> | The name of the parameter to read, as a string. Names are case-sensitive. |
get returns the first value with that name, as a string, or null when the query string has no parameter with that name. That's how MDN's URLSearchParams.get() reference describes it, and the output below shows both cases.
The Example
The example reads name, city and seats from the query string '?name=Ana&city=New%20York&seats=2', checks what type of value seats is, and asks for country, a name the query string doesn't have.
Prerequisites
- Node.js. This guide uses Node.js 26.13.1.
Step 1: Write the Code
Create a file named main.js:

const query = new URLSearchParams('?name=Ana&city=New%20York&seats=2');
console.log(query.get('name'));
console.log(query.get('city'));
console.log(query.get('seats'));
console.log(typeof query.get('seats'));
console.log(query.get('country'));
new URLSearchParams(...) reads the query string once. Each get then looks up one name in it.
Step 2: Run It
node main.js
Ana
New York
2
string
null

The command is the same in zsh, bash and PowerShell.
What Happened
query.get('name')returnedAna, the text aftername=, up to the next&.New%20Yorkcame back asNew York. A web address can't contain a space, so it's written as%20, andgetdecodes it. A+in a query string decodes to a space too:new URLSearchParams('?q=a+b').get('q')is'a b'.seatscame back as2, andtypeofsays it's astring. Every value in a query string is text. Convert it before you calculate with it:query.get('seats') + 1is'21', whileNumber(query.get('seats')) + 1is3.query.get('country')returnednull, because the query string has no parameter namedcountry.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Passing a whole web address, new URLSearchParams('https://example.com/?name=Ana') | get('name') returns null, because the first name it reads is https://example.com/?name | Pass only the part from ? on, or use new URL(address).searchParams.get('name'), which returned Ana |
| Adding a number to a value | query.get('seats') + 1 is '21': the + joins two pieces of text | Convert first: Number(query.get('seats')) + 1 is 3 |
Asking for 'Name' when the query string has name | null: names are case-sensitive | Use the name exactly as it's written in the query string |
| Calling a string method on a value that may be missing | query.get('tags').split(',') throws TypeError: Cannot read properties of null (reading 'split') when there's no tags | Check for null first, or give a default: (query.get('tags') ?? '').split(',') |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
get returns null for a parameter you can see | The name's case is different, or you passed the whole web address | Copy the exact name, and pass only the part from ? on |
A value still has %20 or + in it | The value was cut out of the text by hand instead of read with get | Read it with get, which decodes both |
get returns '', an empty string | The query string has the name but no value, as in ?name= | Treat '' as "no value" wherever you check the result |
FAQ
How do I read a parameter from the current page's address in a browser?
In a browser, window.location.search holds the query string of the page's address, so new URLSearchParams(window.location.search).get('name') reads name from it. The get part works exactly as it does in Node.js.
What if the same name appears twice?
get returns the first value, and getAll returns all of them as an array. For ?tag=red&tag=blue, get('tag') returned 'red' and getAll('tag') returned [ 'red', 'blue' ].
How do I check whether a parameter is there at all?
Use has. For ?name= and for ?debug, has returned true, although get returned '' for both. For a name that isn't in the query string, has returned false.
Conclusion
new URLSearchParams(queryString).get(name) reads one parameter and decodes it for you. Expect a string or null: convert numbers with Number before you calculate, and check for null before you call a string method on the result.
Additional Resources
- URLSearchParams, on MDN.
- URLSearchParams.get(), on MDN.
- Class: URLSearchParams, in the Node.js documentation.
