Get the value of a param from a URL
Parse an absolute URL and read one query parameter.
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
nullKnow 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.
Keep exploring
- Convert Celsius to Fahrenheit — Apply the temperature conversion without rounding the result.
- Convert hex to RGB — Expand #rgb if needed and read the red, green and blue channels.
- Convert URL parameters to object — Read a query string into an object; repeated keys become arrays.