In JavaScript, you can use the URLSearchParams interface to convert a query string into an object. It provides utility methods to work with the query string of a URL.
Pass the query string to the URLSearchParams constructor to turn it into an object instance.
Use the get() method to access query string parameters.
To get a native JavaScript object, pass the object instance to the Object.fromEntries() method.
const qs = `?size=M&price=29&sort=desc`
const params = new URLSearchParams(qs)
console.log(params.get('size')) // M
console.log(params.get('price')) // 29
console.log(params.get('sort')) // desc
// Convert to native JS object
const obj = Object.fromEntries(params)
console.log(obj)
// { size: 'M', price: '29', sort: 'desc' }