// Add trailing slash even when a query is present.
// supported in all modern browsers
let url = new URL("a?foo=bar", "https://example.com");
if (url.pathname[url.pathname.length - 1] != "/") {
url.pathname += "/";
}
// https://example.com/a/?foo=bar
console.log(url.toString());