The urlSearchParams.keys() method is an inbuilt application programming interface of the URLSearchParams class within url module which is used to get the iterator object containing all the name entries only of URL search params object.
Syntax:
const urlSearchParams.keys()
Parameter: This method does not accept any parameter.
Return value: This method returns the iterator object containing all the name entries only of URL search params object.
Below programs illustrates the use of urlSearchParams.keys() method in Node.js:
Example 1: Filename: app.js
// Node.js program to demonstrate the // URLSearchParams.keys() method // Importing the 'url' moduleconst http = require('url'); // Creating and initializing // URLSearchParams objectconst params = new URLSearchParams(); // Appending value in the objectparams.append('A', 'Book');params.append('B', 'Pen');params.append('C', 'Pencile'); // Getting all the name entries only// by using keys() APIconst iterator = params.keys(); // Display result for each name entryconsole.log("list of all the keys");for (const [name] of iterator) { console.log(name);} |
Run app.js file using the following command:
node app.js
Output:
list of all the keys A B C
Example 2:
Filename: app.js
// Node.js program to demonstrate the // URLSearchParams.keys() method // Importing the module 'url'const http = require('url'); // Creating and initializing// URLSearchParams objectconst params = new URLSearchParams(); // Appending value in the objectparams.append('G', '1');params.append('F', '2');params.append('G', '3'); // Getting all the name entries only// by using keys() apiconst iterator = params.keys(); // Display result for each name entryconsole.log("list of all the keys");for (const [name] of iterator) { console.log(name);} |
Run app.js file using the following command:
node app.js
Output:
list of all the keys G F G
Note: The above program will not run on online JavaScript and script editor.
Reference: https://nodejs.org/dist/latest-v14.x/docs/api/url.html#url_urlsearchparams_keys


