Course contentsShow
← JavaScript
Lesson 2312 of 2,72347. Networking PrimitivesFree lesson

The URLSearchParams Constructor and Basics

Create URLSearchParams objects from query strings to parse and manipulate URL query parameters programmatically.

The URLSearchParams Constructor and Basics

You'll learn how to create and work with URLSearchParams objects to easily parse and manipulate URL query parameters.

What is URLSearchParams?

Query strings (like ?name=John&age=30) can be tedious to parse manually. The URLSearchParams constructor creates an object that handles all the parsing, reading, and modifying of these parameters for you. Think of it as a specialized "dictionary" designed specifically for URL query strings.

Creating a URLSearchParams Object

You can create a URLSearchParams instance from a query string:

// From a query string (with or without the leading '?')
const params = new URLSearchParams('?name=Alice&role=admin');
// or
const params2 = new URLSearchParams('name=Alice&role=admin');

You can also pass an object or array of key-value pairs:

// From an object
const params3 = new URLSearchParams({ name: 'Bob', role: 'user' });

// From an array of pairs
const params4 = new URLSearchParams([['name', 'Carol'], ['role', 'guest']]);

Reading Parameters

Use the get() method to retrieve values:

const params = new URLSearchParams('name=David&age=25');
console.log(params.get('name')); // "David"
console.log(params.get('age'));  // "25"
console.log(params.get('missing')); // null

Why This Matters

Before URLSearchParams, developers wrote complex regex or string-splitting logic. Now you get a clean, built-in API that handles encoding, decoding, and edge cases automatically—making URL parameter management straightforward and error-free.

Key Takeaway: URLSearchParams transforms query strings into easy-to-use objects, eliminating manual parsing and providing a clean interface for reading URL parameters.