Skip to main content

Get Query Parameters as Object

Retrieves URL query parameters and converts them into a standard JavaScript object with string keys and values.

Import​

import { getQueryParams } from 'nhb-toolbox';

Function Signature​

function getQueryParams(): Record<string, string>

Usage Examples​

// If the current URL is: https://example.com/?user=alex&role=admin

const params = getQueryParams();
// Returns: { user: "alex", role: "admin" }

API Reference​

Parameters​

NameTypeDescription
(none)(none)No arguments required; uses window.location.search

Returns​

A plain object (Record<string, string>) mapping query parameter names to their string values.

Key Features​

  1. Automatic Extraction: Reads directly from the current URL without manual input.
  2. URL Decoding: Automatically decodes parameter values.
  3. Simple Interface: No configuration or arguments required.

Limitations​

  1. Browser-only: Requires a window environment. Not suitable for Node.js/server-side use.
  2. Only First Values: If a key appears multiple times, only the last value is kept.
  3. String Values Only: All returned values are strings, not numbers or booleans.
  4. Single-level: Does not parse nested or structured parameters.

Notes​

  • Updating the URL (e.g., via History API) will affect the result on the next call.
  • Empty query strings will return an empty object {}.
  • Keys and values are automatically decoded.
  • Reading filters, search terms, or IDs from URLs in single-page applications.
  • Prefilling form fields or search boxes based on current URL parameters.
  • Logging or analytics of URL parameter usage.

Conclusion:
getQueryParams is a lightweight utility for extracting URL query parameters in the browser. It’s most useful for simple scenarios where you need a quick object view of the current URL’s query string.