Hendrik Lammers

Get query params from URL

The URLSearchParams object can be used to easily work with the query parameters of a website URL. Create a new URL object by passing the document.location to the constructor. This newly created object contains a searchParams property which gives access to the query parameters.

// www.website.com?id=foo&age=94
const { searchParams } = new URL(document.location)

searchParams.get('id') // foo
searchParams.has('age') // true
searchParams.forEach(param => console.log(param)) // foo, 94