J—P

Limited availability — accepting new projects starting June 2026.

Build a Search Query String in JavaScript

PHP has http_build_query and I never forget it. JavaScript's URLSearchParams does the same thing but doesn't filter empty values or prepend ?. This helper wraps it into something you can actually use directly:

function buildSearch(params) {
  const query = new URLSearchParams(
    Object.entries(params).filter(([, x]) => x != null && x !== "")
  ).toString()
  return query && "?" + query
}

Credit: Stack Overflow