JavaScript URLSearchParams forEach() Tutorial

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

JavaScript URLSearchParams forEach() Method

The `forEach()` method is a way of iterating through all the entries of a query.

The `forEach()` method takes a reference to a function as its argument and runs that function for each parameter (key/value) of the target query.

Every time the reference function is called, two arguments will be passed to it:

  • The first argument is the value of the query-parameter.
  • The second argument is the key to the query-parameter.

URLSearchParams forEach() Method Syntax:


URLSearchParams forEach() Method Parameter:

This method takes one argument and that is a function which will be executed for every entry (a pair of key/value) of the target query.

URLSearchParams forEach() Method Return Value:

The method does not return a value.

Example: using URLSearchParams forEach() method in JavaScript

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


    console.log(`The value is: ${value} and the key is: ${key}`);



The value is: John and the key is: name

The value is: Doe and the key is: lastName

The value is: Mars and the key is: address

 The value is: Omid and the key is: name

Leave a Reply