Skip to main content

How to Read a Query String Parameter in JavaScript with URLSearchParams.get

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
const <params> = new URLSearchParams(<query string>);
<params>.get(<name>)
PartWhat 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:

main.js, which creates a URLSearchParams object from the query string name=Ana, city=New%20York and seats=2, then logs get for name, city and seats, the typeof of seats, and get for country.

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​

Terminal
node main.js
Output
Ana
New York
2
string
null

A zsh terminal where node main.js prints Ana, New York, 2, string and null.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • query.get('name') returned Ana, the text after name=, up to the next &.
  • New%20York came back as New York. A web address can't contain a space, so it's written as %20, and get decodes it. A + in a query string decodes to a space too: new URLSearchParams('?q=a+b').get('q') is 'a b'.
  • seats came back as 2, and typeof says it's a string. Every value in a query string is text. Convert it before you calculate with it: query.get('seats') + 1 is '21', while Number(query.get('seats')) + 1 is 3.
  • query.get('country') returned null, because the query string has no parameter named country.

Common Mistakes​

MistakeWhat happensDo 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/?namePass only the part from ? on, or use new URL(address).searchParams.get('name'), which returned Ana
Adding a number to a valuequery.get('seats') + 1 is '21': the + joins two pieces of textConvert first: Number(query.get('seats')) + 1 is 3
Asking for 'Name' when the query string has namenull: names are case-sensitiveUse the name exactly as it's written in the query string
Calling a string method on a value that may be missingquery.get('tags').split(',') throws TypeError: Cannot read properties of null (reading 'split') when there's no tagsCheck for null first, or give a default: (query.get('tags') ?? '').split(',')

Troubleshooting​

SymptomLikely causeFix
get returns null for a parameter you can seeThe name's case is different, or you passed the whole web addressCopy the exact name, and pass only the part from ? on
A value still has %20 or + in itThe value was cut out of the text by hand instead of read with getRead it with get, which decodes both
get returns '', an empty stringThe 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​