Reading Query Parameters with Get and Has
You'll learn how to retrieve and check for query parameter values using URLSearchParams methods.
Understanding Get and Has
Once you've created a URLSearchParams object, you need ways to actually read the data it contains. The get() method retrieves the value of a specific parameter, while has() checks whether a parameter exists at all—like asking "do I have a key for this lock?" versus "what does this key open?"
The get() Method
get() accepts a parameter name and returns its value as a string. If the parameter doesn't exist, it returns null:
const params = new URLSearchParams('name=Alice&age=30');
console.log(params.get('name')); // "Alice"
console.log(params.get('age')); // "30"
console.log(params.get('city')); // null
Notice that even numeric-looking values come back as strings—you'll need to convert them if you need numbers.
The has() Method
has() returns a boolean indicating whether a parameter name exists, regardless of its value:
const params = new URLSearchParams('theme=dark&debug=');
console.log(params.has('theme')); // true
console.log(params.has('debug')); // true (even though value is empty)
console.log(params.has('language')); // false
This is especially useful for flags or optional parameters where presence matters more than value.
Why Use Both?
Use has() when you need to distinguish between "parameter not provided" and "parameter provided but empty." Use get() when you actually need the value itself. They work together to give you complete control over query string data.
Key Takeaway: Use get() to retrieve query parameter values (returns null if missing) and has() to check if a parameter exists (returns boolean), giving you flexible ways to inspect URL search parameters.