Get the value of a param from a URL

Parse an absolute URL and read one query parameter.

JavaScript Version 1.0.0 · MIT · .md

get-the-value-of-a-param-from-a-url.js
function getParam({ url, param }) { return new URL(url).searchParams.get(param); }

Input & expected output

Input

{
  "url": "https://example.com/unsubscribe?token=abc123&list=news",
  "param": "token"
}

Expected output

"abc123"

The edge case

Input

{
  "url": "https://example.com/",
  "param": "token"
}

Expected output

null

Know the limits

  • A missing parameter returns null. Repeated parameters return the first value; use getAll for every value.
  • Treat tokens from links as credentials: never log them.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Based on Get the value of a param from a URL from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Uses URL.searchParams directly; validates an absolute URL. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.