Skip to content
Related Articles

Related Articles

Node.js URLSearchParams.toString()

View Discussion
Improve Article
Save Article
  • Last Updated : 14 Oct, 2021
View Discussion
Improve Article
Save Article

In URLSearchParams interface, the toString() method returns a query string which is suitable for use in a URL.

Syntax:

URLSearchParams.toString()

Return:Returns the search parameters serialized as a string(with Characters percent-encoded).

Example 1:




let url = new URL('https://example.com?foo=1&bar=2');
let params = new URLSearchParams(url.search.slice(1));
  
//Add another parameter.
params.append('par', 4);
console.log(params.toString());


Output:

'foo=1&bar=2&par=4'

Example 2: To create parameters directly




// note: params can also be directly created
let url = new URL('https://example.com?par=1&bar=2');
let params1 = url.searchParams;
console.log(params1.toString());
// or even simpler
let params2 = new URLSearchParams('par=1&bar=2');
console.log(params2.toString());


Output:

par=1&bar=2
par=1&bar=2

Supported Browsers:

  • Google Chrome
  • IE
  • Edge
  • Opera
  • Apple Safari
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!