@Aplet123's answer works, but it filters out all falsy values. So, I'd suggest a small change:
function getFilteredObject(obj) {
// we're filtering out all null, undefined and empty strings but not 0 and false
return Object.fromEntries(Object.entries(obj).filter(([k, v]) => ![null, undefined, ""].includes(v)));
}
// you could also use `skipNull: true` as mentioned by others to only skip null values
const url = `/user?${qs.stringify(getFilteredObject({ name, age }))}`;