URLSearchParams

Getting Query String Values (URLSearchParams)

URLSearchParams parses and builds query strings, so you never split on & by hand. Use new URLSearchParams(location.search) for the current page, or url.searchParams, a live view that rewrites url.href as you edit (encoding rules: URL Encoding and Encoding URIs).

Reading, editing and serializing query parametersJavaScript
const url = new URL('https://shop.example/search?q=rock+%26+roll&tag=cd&tag=vinyl&page=2');
const params = url.searchParams;
console.log(params.get('q'), JSON.stringify(params.getAll('tag')), params.get('x'), params.size);
params.set('page', '3');                           // replaces every "page"
params.append('tag', 'lp & more');
params.delete('tag', 'cd');                        // name + value form
console.log(url.href, params.has('tag', 'vinyl'));
Output
rock & roll ["cd","vinyl"] null 4
https://shop.example/search?q=rock+%26+roll&tag=vinyl&page=3&tag=lp+%26+more true

get() returns a string or null, so convert numbers yourself. size (Baseline widely available since September 2023) and the value argument of has() and delete() (Chrome 117 1 , Firefox 115 555 , Safari 17 10 ) are the newest parts. Sync filters to the address bar with history.replaceState(null, '', url) (Accessing History).