URL objects have both a search
property that shows the query parameters as a single string and searchParams
, a URLSearchParams
object with which you can manage the params without having to think about URL encoding or any nastiness.
myUrl.search === "?id=1&q=search%20term"
myUrl.searchParams.get("q") === "search term"
myUrl.searchParams.set("q", "other search term");
// {id: '1', q: 'other search term'}
Object.fromEntries(myUrl.searchParams.entries());