Skip to content

How to get query string values in JavaScript with URLSearchParams

Accessing and modifying query string values using URLSearchParams

The HTTP protocol allows the request to a web page to be made with a query string.

Like this:

https://test.com/?name=roger
https://test.com/hello?name=roger

In this case we have a single query parameter, named name, with the value roger.

You can have multiple parameters, like this:

https://test.com/hello?name=roger&age=20

The parameters passed as a query string are normally used server-side, to generate a proper response. Here’s how you can access query parameters using Node.js.

To access the value of the query inside the browser, using JavaScript, we have a special API called URLSearchParam, supported by all modern browsers

Here is how we can use it:

const params = new URLSearchParams(window.location.search)

Note: don’t pass the full URL as a parameter to URLSearchParams(), but only the query string part of the URL, which you access using window.location.search.

In the case of:

https://test.com/hello?name=roger

window.location.search is equal to the string ?name=roger.

Now that you have the params object, you can query it.

You can check if a parameter was passed:

params.has('test')

You can get the value of a parameter:

params.get('test')

You can iterate over all the parameters, using for..of:

const params = new URLSearchParams(window.location.search)
for (const param of params) {
  console.log(param)
}

A parameter can have more than one value.

In this case, we pass the same parameter name multiple times, like this:

https://test.com/hello?name=roger&name=flavio

We have no way to detect if a parameters is passed more than once. If we use params.get('name'), we’ll only get the first value back.

We can sue params.getAll('name') to get back an array with all the values passed.

In addition to has(), get() and getAll(), the URLSearchParams API offers several other methods that we can use to loop through the parameters:

Other methods to alter the parameters, for use in other JavaScript running in the page (they do not change the URL):

We also have sort() to sort parameters by key value, and we have the toString() method to generate a query string from the values.

We can use append() / set() / delete() to edit the query string, and generate a new one with toString().


download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about platform: