1. Parse a URL
The WHATWG URL constructor parses a string into structured parts you can read and modify.
const url = new URL("https://x.com/p?a=1&b=2");Typorithm · easy
Step 1 of 8 · intro card
Core Modules
The WHATWG new URL("https://x.com/p?a=1") parses a URL into pathname and searchParams. Read params with url.searchParams.get("a"), and use URLSearchParams to build or parse query strings. Prefer URL over the legacy querystring module.
new URL("https://x.com/p?a=1&b=2") parses a string into structured parts: url.pathname is "/p", url.host is "x.com", and url.searchParams is a live URLSearchParams of the query.
Read query parameters with url.searchParams.get("a"), and use a standalone URLSearchParams to build or parse query strings — params.set("c", "3") then params.toString() handles encoding for you.
The WHATWG URL and URLSearchParams are the modern, standards-based APIs; prefer them over the legacy querystring module, which predates them and does no URL-aware parsing.
new URL(...) exposes pathname and searchParams.
url.searchParams.get("a") reads a query parameter.
URLSearchParams builds/parses query strings; prefer URL over legacy querystring.
const url = new URL("https://x.com/p?a=1&b=2");
url.pathname; // "/p"
url.searchParams.get("a"); // "1"
const params = new URLSearchParams("a=1&b=2");
params.get("b"); // "2"
params.set("c", "3");
params.toString(); // "a=1&b=2&c=3"Connect each part of the code to the concept before you type.
The WHATWG URL constructor parses a string into structured parts you can read and modify.
const url = new URL("https://x.com/p?a=1&b=2");url.pathname exposes just the path, here "/p", without the host or query.
url.pathname;searchParams is a URLSearchParams; .get("a") returns the value of query parameter a.
url.searchParams.get("a");URLSearchParams parses or builds query strings without manual escaping or string splitting.
const params = new URLSearchParams("a=1&b=2");.toString() serializes the params back into a properly encoded query string.
params.toString();Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.