JavaScript URLSearchParams get() Tutorial

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

JavaScript URLSearchParams get() Method

When you have a query, you might want to get the value of a key in that query! The `get()` method is used for this purpose.

Basically, this method takes one argument and that is the key that we want to get its associated value.

URLSearchParams get() Method Syntax:

URLSearchParams.get(name)

URLSearchParams get() Method Parameter:

The `get()` method takes a key as its argument and returns the first value associated with the given key.

URLSearchParams get() Method Return Value:

The return value of this method is the value associated with the key that we’ve set as the argument of the method.

Note: if the given key does not exist in the query, the return value of this method will be `null`.

Example: using URLSearchParams get() method in JavaScript

const searchParams = new URLSearchParams("?name=John&lastName=Doe&address=Mars&name=Omid");

const res = searchParams.get("name");

console.log(res);

Output:

John

Here, the key `name` exists in the query and actually in two different positions. So after running the program, the `get()` method returned the value of the first `name` in this query, which is `John`.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies