Typorithm · easy

URL & Query Parsing

node-url-querystringnodejsurlquery-stringsearch-params
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn URL & Query Parsing

0%

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.

01

new URL(...) exposes pathname and searchParams.

02

url.searchParams.get("a") reads a query parameter.

03

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"

Read the implementation

Connect each part of the code to the concept before you type.

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");

2. Path part

url.pathname exposes just the path, here "/p", without the host or query.

url.pathname;

3. Read a param

searchParams is a URLSearchParams; .get("a") returns the value of query parameter a.

url.searchParams.get("a");

4. Build params

URLSearchParams parses or builds query strings without manual escaping or string splitting.

const params = new URLSearchParams("a=1&b=2");

5. Serialize

.toString() serializes the params back into a properly encoded query string.

params.toString();

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

URL & Query Parsing: Lesson & Practice · Typorithm | ApplyDjinn