In this section, we will learn what the keys() method is and how to use it in JavaScript.
JavaScript URLSearchParams keys() Method
The `keys()` method is used to return all the keys in a query.
Note: the method takes no argument.
The return value of this method is actually an iterator object that contains all the keys in the target query.
URLSearchParams keys() Method Syntax:
searchParams.keys();
URLSearchParams keys() Method Parameter:
The method does not take an argument.
URLSearchParams keys() Method Return Value:
The return value of this method is an iterator that could be used to iterate through all the keys of the target query.
Example: using URLSearchParams keys() method in JavaScript
const searchParams = new URLSearchParams("?name=John&lastName=Doe&address=Mars&name=Omid"); for (const key of searchParams.keys()){ console.log(key); }
Output:
name lastName address name