Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Node.js URLSearchParams.keys()
Next article icon

Node.js URLSearchParams.toString()

Last Updated : 14 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Next Article
Node.js URLSearchParams.keys()

D

DeepakDev
Improve
Article Tags :
  • JavaScript
  • Node.js
  • Web Technologies
  • Node-URL

Similar Reads

    Methods

    • Node.js URL() Method
      The 'url' module provides utilities for URL resolution and parsing. The getters and setters implement the properties of URL objects on the class prototype, and the URL class is available on the global object. The new URL() (Added in v7.0.0, v6.13.0) method is an inbuilt application programming inter
      3 min read

    • Node url.toString() Method
      The url.toString() method is an inbuilt application programming interface(API) of the URL module within the Node.JS. The url.toString() method is used to return the serialized URL. The returned value is equivalent to that of url.href and url.toJSON(). Syntax: url.toString() Parameters: This method a
      1 min read

    • Node.js URL.toJSON() Method
      The url.toJSON() method in the node.js URL module is used to return the serialized URL of the URL object. The return value of this method is equivalent to the URL.href and url.toString() methods. If an URL object is serialized using JSON.stringify() method then it is called automatically. Syntax: ur
      2 min read

    • Node.js url.domainToUnicode() Method
      The url.domainToUnicode() method is an inbuilt application programming interface of class URL with in url module which is used to get the domain for the particular ASCII value. Syntax: const url.domainToUnicode(domain) Parameter:s This method takes ASCII value as a parameter which is going to be con
      2 min read

    API

    • Node.js URL.hash API
      The url.hash is an inbuilt application programming interface of class URL within url module which is used to get and set the fragment portion of the URL. Syntax: url.hash Return value: It gets and sets the fragment portion of the URL. Below programs illustrate the use of url.hash Method: Example 1:
      1 min read

    • Node.js URL.host API
      The url.host is an inbuilt application programming interface of class URL with in url module which is used to get and set the host portion of the URL. Syntax: const url.host Return value: It gets and sets the host portion of the URL.Below programs illustrates the use of url.host Method:Example 1: C/
      1 min read

    • Node.js URL.origin API
      url.origin is an inbuilt application programming interface(API) of the URL class within the url module. url.origin API is used to gets the read-only serialization of the URL’s origin. Syntax: url.origin url : It is an object created by URL constructor. Example 1: C/C++ Code //Importing the url modul
      1 min read

    • Node.js URL.username API
      URL.username is an inbuilt application programming interface(API) of the URL class within Node.JS. URL.username API is used to get and set the username of the URL. Syntax: url.username URL: It is an object created by a URL constructor. Example 1: (Getting the username of URL) C/C++ Code //Creating a
      1 min read

    • Node.js URL.search API
      URL.search is an inbuilt application programming interface(API) of the URL class within the Node.JS. URL.search API is used to get and set the query part of URL. Syntax: url.search url : It is an object created by URL constructor. Example 1: (Getting query string of the URL) C/C++ Code //Creating an
      1 min read

    • Node.js URL.port API
      The url.port is an inbuilt application programming interface of class URL within url module which is used to get and set the port portion of the URL.the port value may be a number or a string containing a number in the range 0 to 65535 (inclusive). Setting the value to the default port of the URL ob
      3 min read

    • Node.js URL.pathname API
      The url.pathname is an inbuilt application programming interface of class URL with in url module which is used to get and set the pathname portion of the URL. Syntax: const url.pathname Return value: It gets and sets the pathname portion of the URL. Below programs illustrate the use of url.pathname
      1 min read

    • Node.js URL.password API
      The url.password is an inbuilt application programming interface of class URL with in url module which is used to get and set the password portion of the URL. Syntax: const url.password Return value: It gets and sets the password portion of the URL.Below programs illustrate the use of url.password M
      2 min read

    • Node.js URL.href API
      The url.href is an inbuilt application programming interface of class URL with in the url module which Gets and sets the serialized URL. Getting the value of the href property is equivalent to calling the url.toString() method.Setting the value of this property to a new value is equivalent to creati
      2 min read

    • Node.js URL.hostname API
      The url.hostname is an inbuilt application programming interface of class URL with in url module which is used to get and set the hostname portion of the URL. The key difference between url.host and url.hostname is that url.hostname does not include the port. Syntax: const url.hostname Return value:
      1 min read

    • Node.js URL.pathToFileURL API
      This URL.pathToFileURL function converts the path to a file and ensures that the URL control characters (/, \, : ) are correctly appended/adjusted when converting the given path into a File URL. Syntax: url.pathToFileURL(path) Parameters: This function accepts a single parameter path that holds the
      2 min read

    • Node.js URL.fileURLToPath API
      This URL.fileURLToPath function decodes the file URL to a path string and ensures that the URL control characters (/, %) are correctly appended/adjusted when converting the given file URL into a path. Syntax: url.fileURLToPath( url ) Parameters: This function accepts single parameter url which holds
      1 min read

    • Node.js url.parse(urlString, parseQueryString, slashesDenoteHost) API
      The url.parse() method takes a URL string, parses it, and it will return a URL object with each part of the address as properties. Syntax: url.parse( urlString, parseQueryString, slashesDenoteHost) Parameters: This method accepts three parameters as mentioned above and described below: urlString: It
      2 min read

    • Special Schemes of Node.js URL.protocol API
      The url.protocol is an inbuilt application programming interface of class URL within URL module which is used to get and set the protocol scheme of the URL. Syntax: const url.protocol Return value: It get and set protocol scheme of the URL Example 1: This example changes the special protocols to hyp
      3 min read

    • Node.js URL.protocol API
      The url.protocol is an inbuilt application programming interface of class URL within url module which is used to get and set the protocol portion of the URL. When a URL is parsed using one of the special protocols, the url.protocol property may be changed to another special protocol but cannot be ch
      2 min read

    • Node.js URL.domainToASCII
      The url.host is an inbuilt application programming interface of class URL with in url module. It returns the Punycode ASCII serialization of the domain. If domain is an invalid domain, the empty string is returned. Syntax : const url.domainToASCII Domain value : string Return value : string Example
      1 min read

    • Node.js URL.domainToUnicode
      The url.domainToUnicode is an inbuilt application programming interface of class URL with in url module. It returns the Unicode serialization of the domain. If the domain is invalid, the empty string is returned. Syntax : const url.domainToASCII Domain value : string Return value : string Example :
      1 min read

    • Node.js URL.resolve(from,to) API
      The url.resolve(from, to) is inbuilt method of class URL that resolves a target URL relative to a base URL. Syntax: url.resolve(from, to); Where, from: (type:String) The base URL being resolved against. to : (type:String) The "href" URL being resolved. Return value: It returns the resolved URL by gi
      1 min read

    • Node.js URL.format(urlObject) API
      The URL.format(urlObject) is the inbuilt API provided by URL class, which takes an object or string and return a formatted string derived from that object or string. Syntax: const url.format(urlObject) If the urlObject is not an object or string, then it will throw a TypeError. Return value: It retu
      4 min read

    • Node.js URL.format API
      With the help of url.format()method, we are able to format the hostname according to our need. We have different types of other parameters which we can use to generate the hostname or to change the hostname as required. Syntax : url.format(URL[, options]) Parameter : auth is a boolean value if true
      2 min read

    URLSearchParams

    • Node.js URLSearchParams.append()
      In URLSearchParams interface, the append() method adds a key/value pair specified by user. Syntax: URLSearchParams.append(name, value) Parameters: name - Input the parameter name to append value - Input the parameter value to append Example1: let url = new URL('https://example.com?par=1&par1=3')
      1 min read

    • Node.js URLSearchParams.delete()
      In URLSearchParams interface, the delete() method deletes the parameter specified by user and all its associated values. Syntax: URLSearchParams.delete(name) Parameters: name - The name of the parameter to be deleted. Example 1: let url = new URL('https://example.com?par=1&bar=2&par=3'); let
      1 min read

    • Node.js URLSearchParams.entries()
      In the URLSearchParams interface, the entries() method returns an iterator that allows iterating through all the key/value pairs present in the object. The key/value is USVString objects. Syntax: searchParams.entries(); Return: Iterator, iterating over all key-value pairs. Example 1: C/C++ Code // C
      1 min read

    • Node.js URLSearchParams.forEach()
      In URLSearchParams interface, the foreach() method returns an iterator which allows to iterate through all values contained in this object with the help of a callback function. Syntax: searchParams.forEach(callback); Return: It does not return anything, used for invoking the function and iterating t
      1 min read

    • Node.js URLSearchParams.get()
      In URLSearchParams interface, the get() method returns the first value of the input search parameter. Syntax: URLSearchParams.get(name) Returns:The string will be returned if the name-value pair is found, else null will be returned. Parameters: name - Input the name of the parameter. Example1: When
      1 min read

    • Node.js urlSearchParams.get() Method
      The urlSearchParams.get() method is an inbuilt application programming interface of class URLSearchParams within url module which is used to get the value for particular name entry present in the URL search params object. Syntax: const urlSearchParams.get( name ) Parameter: This method takes the nam
      2 min read

    • Node.js URLSearchParams.getAll()
      In URLSearchParams interface, the getAll() method returns all the values of the input search parameter in the form of an array. Syntax: URLSearchParams.getAll(name) Returns: An array of string according to the name-value pairs, else an empty array will be returned. Parameters: name - Input the name
      1 min read

    • Node.js URLSearchParams.sort()
      In the URLSearchParams interface, the sort() method helps to sort all keys/pairs in place. The sort criteria are unicode points of the keys. This method uses a stable sorting algorithm. Syntax: searchParams.sort(); Return: Sorted order of existing name-value pairs in place by their names. Example 1:
      1 min read

    • Node.js urlSearchParams.toString() Method
      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.Retu
      2 min read

    • Node.js urlSearchParams.keys() Method
      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
      2 min read

    • Node.js urlSearchParams.values()
      In the URLSearchParams interface, the values() method returns an iterator which allows us to iterate through all the values present in the object Syntax: searchParams.values(); Return: Returns an ES6 Iterator over the values of each name-value pair. Example 1: In this example, we will see the use of
      1 min read

    • Node.js URLSearchParams.set()
      In URLSearchParams interface, the set() method sets the value given as an input parameter.If there are several values matching the input parameter then it deletes the others and if the value does not exist then it creates it. Syntax: URLSearchParams.set(name, value) Parameters: name - Input the name
      1 min read

    • Node.js 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: let url = new URL('https://example.com?foo=1&
      1 min read

    • Node.js URLSearchParams.keys()
      In URLSearchParams interface, the keys() method returns an Iterator which allows us to iterate through all the keys present in the object. Syntax: searchParams.keys(); Return:Returns an ES6 Iterator over the names of each name-value pair. Example 1: var searchParams = new URLSearchParams("keyA=
      1 min read

    • Node.js URLSearchParams.has() Method
      In URLSearchParams interface, the has() method returns a Boolean which tells us that if the parameter with input name exists it will return true, else false. Syntax: var Bool = URLSearchParams.has(name) Returns: True - if name present, else it will return False. Parameters: name - Input the name of
      1 min read

    urlObject

    • Node.js urlObject.auth API
      With the help of urlObject.auth() method, we can find the authentication parameter within the hostname. This method returns the string of parameters. Syntax : urlObject.auth() Return: Returns the string of authentication parameters. Example 1: In this example with the help of urlObject.auth() method
      1 min read

    • Node.js urlObject.hash API
      Before we go on to learn about URL objects API we need to know a brief about URL. If you ever wanted to provide utilities so as to parse or resolute your URL, we can do it through the URL Module. We can divide the URL Module into two parts:- These are- URL String and URL Object. The urlObjectHash is
      1 min read

    • Node.js urlObject.host API
      The utilities for URL resolution and parsing is provided by the URL module. A URL string is a structured string that contains various multiple meaningful components. When parsed, a URL object is returned that contains properties for each of these components. url.host() return the host name in the ur
      1 min read

    • Node.js urlObject.search API
      The urlObject.search() method in Node is used to get the search query within the hostname which is followed by the '?' character. Syntax : urlObject.search() Return : Returns the search query after '?' character. Example #1 : In these examples, we have shown how the urlObject.search() method is able
      1 min read

    • Node.js urlObject.protocol API
      With the help of urlObject.protocol() method, we can find the name of protocol which is used by the given hostname. Syntax : urlObject.protocol() Return : Returns the protocol used (i.e. - http, https, ftp, etc.) Example #1 : In this example, with the help of urlObject.protocol() method we are able
      1 min read

    • Node.js urlObject.href API
      The urlObject.href API is used to return the complete URL string along with the protocols(HTTP) and pathname or other search terms. Syntax: urlObject.hrefFor example: 'http://www.geeksforgeeks.com/login/password.html' Here, Protocol = http Path = /login Host = 'www' File Name = password.html Example
      1 min read

    • Node.js urlObject.query API
      The urlObject.query is the query string returned without the ASCII question mark (?) or an object returned by the query string module named as parse() method. The url.parse() method is used to check whether the query is a string or an object. Basically, the argument (parseQueryString) that is passed
      2 min read

    • Node.js urlObject.slashes API
      The urlObject.slashes property is used when one wants to check whether there is a need for ASCII valued forward-slash character (/) after the colon (:) in the protocol (HTTP or HTTPS) of a URL or not. It returns the Boolean value. Syntax: urlObject.slashes Return Value: The urlObject.slashes propert
      1 min read

    • Node.js urlObject.port API
      The urlObject.port() method in Node is used to get the numeric port portion of the host component within the hostname. The URL’s port number is returned otherwise None if the port number is not present in the URL. Syntax: urlObject.port() Return: Returns the URL port number or None Example 1: In the
      1 min read

    • Node.js urlObject.pathname API
      With the help of urlObject.pathname() method, we can find the name of the path which is used by the given hostname. This contains all the things starting from the host (with the port) and before the beginning of the query or hash components, which are delimited by one of the ASCII question mark (?)
      1 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences