Skip to content
Related Articles

Related Articles

Node.js urlSearchParams.toString() Method
  • Last Updated : 09 Jul, 2020

The urlSearchParams.toString() method is an inbuilt application programming interface of class URLSearchParams within url module which is used to get the object of uri search params object as a string.

Syntax:

const urlSearchParams.toString()

Parameter: This method does not accept any parameter.

Return value: This method returns the object of uri search params object as a string.

Below programs illustrates the use of urlSearchParams.toString() method in Node.js:



Example 1: Filename app.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the 
// URLSearchParams.toString() method
   
// Importing the module 'url'
const http = require('url');
  
// Creating and initializing 
// URLSearchParams object
const params = new URLSearchParams();
  
// Appending value in the object
params.append('A', 'Book');
params.append('B', 'Pen');
params.append('A', 'Pencil');
  
// Getting string reprsentation
// by using toString() api
const value = params.toString();
  
// Display the result
console.log("String representation"
      + " of oject : " + value);

chevron_right


Run app.js file using the following command:

node app.js

Output:

String representation of oject : A=Book&B=Pen&A=Pencil

Example 2:
Filename app.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the 
// URLSearchParams.toString() method
   
// Importing the module 'url'
const http = require('url');
  
// Creating and initializing 
// URLSearchParams object
const params = new URLSearchParams();
  
// Appending value in the object
params.append('geeks', 'code');
params.append('for', 'eat');
params.append('geeks', 'sleep');
  
// Getting string reprsentation
// by using toString() api
const value = params.toString();
  
// Display the result
console.log("String representation"
      + " of oject : " + value);

chevron_right


Run app.js file using the following command:

node app.js

Output:

String representation of oject : geeks=code&for=eat&geeks=sleep

Reference: https://nodejs.org/dist/latest-v14.x/docs/api/url.html#url_urlsearchparams_tostring

My Personal Notes arrow_drop_up
Recommended Articles
Page :