JavaScript URLSearchParams getAll() Tutorial

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

JavaScript URLSearchParams getAll() Method

In a query, there might be one key but multiple values associated with that key.

The `getAll()` method is used to get all the values associated with a key.

Basically, this method takes one argument, which is the key that we want to get all its associated values and after that, this method will look into the target query looking for any value associated with the given key.

URLSearchParams getAll() Method Syntax:

URLSearchParams.getAll(name)

URLSearchParams getAll() Method Parameter:

The `getAll()` method takes a key as its argument and returns ALL the values associated with the given key.

URLSearchParams getAll() Method Return Value:

The return value of this method is an array that contains all the values related to the target key.

Note: in case there wasn’t any value associated with the given key, the return value of this method will be an empty array.

Example: using URLSearchParams getAll() method in JavaScript

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

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

console.log(res);

Output:

John,Omid

Leave a Reply