easypredictionjavascript~5 min
Predict URL and URLSearchParams Output
Objective: Know which URL parts the platform exposes and how repeated query keys behave.
Predict what each log line prints for this URL manipulation.
- Write one value per line, in print order.
Read the code, then type what it prints
const url = new URL("https://shop.example.com:8080/a/b?x=1&y=2#top");
console.log(url.origin);
console.log(url.pathname);
console.log(url.searchParams.get("y"));
console.log(url.hash);
console.log(url.port);
url.searchParams.append("x", "3");
console.log(url.searchParams.getAll("x").join(","));
console.log(url.search);Output
> Nothing has run yet.
Interview follow-up questions
- How does `set` differ from `append`?
- Which URL parts are sent to the server and which stay in the browser?
- Why use URLSearchParams instead of string concatenation?