const url2 = new URL("https://example.com/login?user=someguy&page=news"); console.log(getVariable(url2, 'user')) // "someguy" function getVariable(url, key) { url = new URL(url); return url.searchParams.get(key) }