---
title: "How to get the values from GET parameters in JavaScript"
description: "GET parameters are commonly used in web development to pass data through the URL. They play an essential role in tasks such as filtering search results…"
url: https://www.danny.engineering/answer/javascript/how-to-get-the-values-from-get-parameters-in-javascript
author: Danny
published: 2025-02-04T00:00:00.000Z
updated: 2025-02-04T19:37:00.000Z
tags: ["Javascript"]
---

# How to get the values from GET parameters in JavaScript

GET parameters are commonly used in web development to pass data through the URL. They play an essential role in tasks such as filtering search results, tracking user actions, or sharing specific views of a web application. Getting and manipulating these parameters in JavaScript is a frequent need for developers working on interactive front-end applications.

## Problem

When building web applications, you often need to retrieve data encoded in the query string (the portion of the URL after the `?` character). For example, consider the following URL:

```bash 
https://example.com/products?category=electronics&sort=price
```
In this case, the query string contains two parameters: `category=electronics` and `sort=price`. You may want to extract these values in your JavaScript code to display relevant content or perform certain actions like API calls or UI updates.

- Parsing the query string correctly to handle potential encoding, such as special characters or spaces.
- Accessing individual parameters in a reliable way.
- Working with cases where certain parameters are optional or `undefined`.

## Solution

There are multiple ways to extract GET parameter values from the query string in JavaScript. Below are two common approaches.

### Using `URLSearchParams`
The `URLSearchParams` interface simplifies the process of working with query strings. It allows you to retrieve GET parameters in a structured and easy-to-use manner.

- The `URLSearchParams` constructor takes a query string and provides helper methods like `.get()` to access specific parameters by name.

```javascript
// Example URL: https://example.com/products?category=electronics&sort=price

// Step 1: Get the full query string
const queryString = window.location.search; // Output: "?category=electronics&sort=price"

// Step 2: Create a URLSearchParams instance
const params = new URLSearchParams(queryString);

// Step 3: Retrieve specific parameter values
const category = params.get('category'); // "electronics"
const sort = params.get('sort');         // "price"

// Log the results
console.log(`Category: ${category}, Sort: ${sort}`);
```

**Optional Features:**
- Check if a parameter exists: `params.has('key')`
- Iterate over all key-value pairs:
```javascript
params.forEach((value, key) => {
  console.log(`${key}: ${value}`);
});
```



- Ensure browser compatibility if supporting outdated browsers. `URLSearchParams` is widely supported in modern browsers but may not work in older versions of Internet Explorer.

### Manual Parsing with String Methods
For older browser support or highly customised behaviour, you can manually parse the query string using JavaScript's string methods like `split()` and `decodeURIComponent()`.

- Use `window.location.search` to retrieve the query string.
- Remove the leading `?` and split the string into key-value pairs.
- Map the pairs into an object for easy lookup.

```javascript
// Example URL: https://example.com/products?category=electronics&sort=price

// Step 1: Get the query string and remove the "?"
const queryString = window.location.search.substring(1); // "category=electronics&sort=price"

// Step 2: Split into key-value pairs
const paramsArray = queryString.split('&'); // ["category=electronics", "sort=price"]

// Step 3: Convert to an object
const params = {};
paramsArray.forEach(pair => {
  const [key, value] = pair.split('=');
  params[key] = decodeURIComponent(value); // Handle encoded characters
});

// Access specific parameters
console.log(`Category: ${params.category}, Sort: ${params.sort}`);
```

- This approach requires more manual effort and is error-prone if the query string format is inconsistent.
- Performance may be slightly worse for lengthy query strings compared to `URLSearchParams`.

### Further Considerations

1. **Handling Edge Cases:**
   - Always account for parameters that may be missing or empty. For example, `params.get('key')` returns `null` if the parameter doesn’t exist.
   - Use `decodeURIComponent` to handle special characters (e.g., spaces encoded as `%20`).

2. **Security Implications:**
   - Be cautious when using input from the query string as it may be manipulated by users.
   - Avoid directly embedding parameters into your UI or making database queries without sanitisation.

3. **Performance:**
   - For most modern applications, `URLSearchParams` is the recommended method as it is efficient and built into the language.
   - Manual parsing can still be useful in environments with limited modern browser support.

4. **Alternative Scenarios:**
   - For back-end environments like Node.js, libraries such as [querystring](https://www.npmjs.com/package/query-string) or [qs](https://github.com/ljharb/qs) are more suitable for robust query parsing.

### Related Resources

- [MDN Web Docs: URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
- [MDN Web Docs: window.location](https://developer.mozilla.org/en-US/docs/Web/API/Window/location)
- [Node.js `querystring` Module Docs](https://nodejs.org/api/querystring.html)
