Skip to content
Justice Tech School

URL and URLSearchParams

The URL class parses addresses and URLSearchParams handles queries.

Example
const u = new URL("https://x.com:8080/a/b?q=js&page=2#top");
console.log(u.hostname, u.port, u.pathname, u.hash);
console.log(u.searchParams.get("q"), [...u.searchParams.keys()]);
u.searchParams.set("page", "3");
console.log(u.href);

Result

x.com 8080 /a/b #top
js [ 'q', 'page' ]
https://x.com:8080/a/b?q=js&page=3#top

Exercise

Read a query value.

u.searchParams.("q")

Quiz Test

What is u.pathname?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.