Syntax
new URL(urlString)
new URLSearchParams(queryString)Examples
Parsing a URL
Breaking a URL into its structured parts.
const url = new URL("https://example.com:8080/products?category=shoes&sort=price#reviews");
console.log(url.protocol); // "https:"
console.log(url.hostname); // "example.com"
console.log(url.port); // "8080"
console.log(url.pathname); // "/products"
console.log(url.search); // "?category=shoes&sort=price"
console.log(url.hash); // "#reviews"Reading and Modifying Query Parameters
Working with query strings without manual parsing or string concatenation.
const url = new URL("https://example.com/search?q=javascript&page=1");
console.log(url.searchParams.get("q")); // "javascript"
console.log(url.searchParams.get("page")); // "1"
url.searchParams.set("page", "2");
url.searchParams.append("sort", "recent");
console.log(url.toString());
// "https://example.com/search?q=javascript&page=2&sort=recent"Building Query Strings from Scratch
Using URLSearchParams standalone, without a full URL.
const params = new URLSearchParams();
params.set("name", "Alice Smith"); // spaces are correctly encoded
params.set("role", "admin");
console.log(params.toString()); // "name=Alice+Smith&role=admin"
fetch(`/api/users?${params.toString()}`);Best practices
- Use the URL and URLSearchParams objects instead of manually splitting and concatenating query strings - they correctly handle encoding edge cases
- Use searchParams.set() to overwrite a parameter and searchParams.append() to add an additional value for the same key
- Always construct URLs this way when building requests with dynamic query parameters, to avoid manual encoding bugs
- Remember getAll() (not get()) is needed to retrieve multiple values for the same parameter key
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Data Types
JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers: addition (+), subtraction (-), multiplication (*), division (/), remainder/modulo (%), and exponentiation (**). The + operator also performs string concatenation when either operand is a string. Increment (++) and decrement (--) adjust a variable by one.
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Data Types
JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers: addition (+), subtraction (-), multiplication (*), division (/), remainder/modulo (%), and exponentiation (**). The + operator also performs string concatenation when either operand is a string. Increment (++) and decrement (--) adjust a variable by one.