Node | URLSearchParams.toString()

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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());

chevron_right


Output:

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

Example 2: To create parameters directly

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());

chevron_right


Output:

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

Supported Browsers:

  • Google Chrome
  • IE
  • Edge
  • Opera
  • Apple Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.