URL
Parses and builds complete URLs.
URL and URLSearchParams help you read, build and modify web addresses without fragile string splitting.
const url = new URL("https://example.com/search?q=javascript");
url.searchParams.set("page", "2");
console.log(url.toString());
JSON & Structured Data
The URL constructor parses a full address into reliable parts such as origin, pathname and search parameters.
URLSearchParams handles query string encoding for you. It is much safer than concatenating ?q= and &page= by hand.
These APIs are useful in browser routing, filters, API requests, share links and state stored in the address bar.
Parses and builds complete URLs.
Reads and writes query parameters.
set replaces a value; append adds another value.
Special characters are encoded automatically.
Examples
const params = new URLSearchParams();
params.set("q", "javascript basics");
const path = `/search?${params.toString()}`;
const path = "/search?q=" + query + "&page=" + page; // Spaces, ampersands and special characters can break this.
Code patterns
These patterns focus on the data boundaries you will use constantly: JSON, URLs, forms, files, fetch objects and binary buffers.
Get a parameter by name.
const url = new URL(location.href);
const query = url.searchParams.get("q");
Update one query value safely.
const url = new URL("https://example.com/search");
url.searchParams.set("q", "javascript basics");
Use append for multi-select filters.
const params = new URLSearchParams();
params.append("tag", "html");
params.append("tag", "css");
Combine URL and query parameters.
const url = new URL("/api/records", location.origin);
url.searchParams.set("limit", "10");
url.searchParams.set("status", "ready");
Rules that matter
Structured data becomes reliable when every boundary is explicit: text to object, form to values, URL to parameters, response to JSON and bytes to meaning.
It gives you origin, pathname, search and hash safely.
It handles encoding and repeated values.
set replaces the current parameter.
append supports multiple values with the same key.
Query parameters are text.
String splitting breaks on edge cases.
Production thinking
URLs are user-facing state. Building them safely keeps links shareable, readable and robust.
Stable URLs help users share, bookmark and revisit filtered states.
Production code should centralize query building for filters, pagination and API calls.
Clean, crawlable URLs matter for indexed pages and canonical route handling.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Structured data is safe only when you know where it came from, what shape it has and what conversion happened before use.