Skip to main content Accessibility Feedback


Get the value of a query string from a URL.

Source Code


let url = '';

// returns "chicken"
let thisOne = getParam('this', url);

// returns "sandwich"
let thatOne = getParam('that', url);

// returns "tomato"
let topping = getParam('topping', url);

// returns "spicy mayo"
let anotherOne = getParam('another', url);

The helper function

 * Get the value of a query string from a URL
 * (c) Chris Ferdinandi, MIT License,
 * @param  {String} param The parameter to get the value of
 * @param  {String} url   The URL to get the value from [optional]
 * @return {String}       The value
function getParam (param, url = window.location) {
	let params = new URL(url).searchParams;
	let val = params.getAll(param);
	if (val.length > 1) return val;
	return val[0];

How it works

Learn more about the URL and URLSearchParams APIs.

Find this useful? You can support my work by purchasing an annual membership.