function readProductFilters(locationHref = window.location.href) { const url = new URL(locationHref); const params = url.searchParams; return { category: params.get("category") ?? "all", page: Number(params.get("page") ?? "1"), tags: params.getAll("tag"), preview: params.has("preview"), }; } const filtersWithQuery = readProductFilters( "https://www.example.com/products?category=books&page=2&tag=javascript&tag=web&preview", ); const filtersWithoutQuery = readProductFilters("https://www.example.com/products"); console.log("with query:"); console.log(JSON.stringify(filtersWithQuery, null, 2)); console.log("without query:"); console.log(JSON.stringify(filtersWithoutQuery, null, 2));