JavaScript URLSearchParams has() Tutorial

In this section, we will learn what the has() method is and how to use it in JavaScript.

JavaScript URLSearchParams has() Method

The `has()` method is used to see if the target query has a specific key.

The method takes one argument and that is the name of the key that we want to search for in the query.

The return value of this method is of type Boolean. If the value is `true` that means the key is in the query. But if the value was `false` it means the query does not have such key in it.

URLSearchParams has() Method Syntax:

var hasName = URLSearchParams.has(name)

URLSearchParams has() Method Parameter:

The method takes one argument and that is the key we want to know if the target query has it or not.

URLSearchParams has() Method Return Value:

The return value of this method is a boolean value.

  • If the target query has the specified key, the return value is “true”.
  • If the target query does not have the specified key, then the return value of this method becomes false.

Example: using URLSearchParams has() method in JavaScript

const searchParams = new URLSearchParams("?name=John&lastName=Doe&[email protected]&address=mars");

if (searchParams.has('address')){

    console.log("The key 'address' does exist in the query");

}else{

    console.log("There's no such key in the query");

}

Output:

The key 'address' does exist in the query

Leave a Reply