Query strings are structured data, not text to edit with ad-hoc concatenation. URLSearchParams handles encoding, repeated keys, and separators so a filter update does not accidentally damage the rest of the address.

const url = new URL(window.location.href);
url.searchParams.set("category", "css & layout");
url.searchParams.delete("page");
history.replaceState(null, "", url);

Know when set and append differ

  • set replaces all values for a key with one value.
  • append adds another value, which is useful when a parameter can appear more than once.
  • getAll returns every value; get returns only the first.

Preserve the URL's other parts

Construct a URL from the current location, update only the parameters your interface owns, and keep the pathname and hash intact. Avoid putting private information such as credentials into a query string, where it can be copied into logs and referrers.

Use pushState when a change should create a Back-button step; use replaceState when it should only update the current history entry.