Course contentsShow
← Web Development
Lesson 952 of 2,98217. URL and History ManagementFree lesson

Reading Query Parameters

Using get(), getAll(), and has() methods to retrieve parameter values from URLs.

Reading Query Parameters

You'll learn how to extract specific values from URL query strings using the get(), getAll(), and has() methods of URLSearchParams.

The Core Idea

Once you've created a URLSearchParams object, you need ways to read individual parameter values. The API provides three essential methods: get() retrieves a single value, getAll() retrieves all values for parameters that appear multiple times, and has() checks if a parameter exists at all.

Think of these methods like looking up entries in a dictionary: has() checks if a word exists, get() reads its definition, and getAll() finds every definition if the word appears in multiple places.

The Three Reading Methods

get(name)

Returns the first value associated with a parameter name, or null if it doesn't exist:

const params = new URLSearchParams('color=blue&size=large');
console.log(params.get('color')); // "blue"
console.log(params.get('price')); // null (doesn't exist)

getAll(name)

Returns an array of all values for a parameter (useful when the same parameter appears multiple times):

const params = new URLSearchParams('tag=javascript&tag=web&tag=tutorial');
console.log(params.getAll('tag')); // ["javascript", "web", "tutorial"]
console.log(params.getAll('missing')); // [] (empty array)

has(name)

Returns true or false to check if a parameter exists:

const params = new URLSearchParams('debug=true&mode=test');
console.log(params.has('debug')); // true
console.log(params.has('admin')); // false

These methods are case-sensitive: get('Name') is different from get('name').

Key Takeaway: Use get() for single values, getAll() when parameters might repeat, and has() to check existence before reading—this prevents errors and makes your code more robust.