Node.js URLsearchParams API Last Updated : 10 Jul, 2020 Comments Improve Suggest changes 6 Likes Like Report Node.js is an open-source project widely used for the development of dynamic web applications. The URLSearchParams API in Node.js allows read and write operations on the URL query. The URLSearchParams class is a global object and used with one of the four following constructors. Constructors: new URLSearchParams(): No argument constructor instantiates a new empty URLSearchParams object. new URLSearchParams(string): Accepts a string as an argument to instantiate a new URLSearchParams object. javascript var params = new URLSearchParams('user=abc&q=xyz'); console.log(params.get('user')); console.log(params.get('q')); Output: abc xyz new URLSearchParams(obj): Accepts an object with a collection of key-value pairs to instantiate a new URLSearchParams object. The key-value pair of obj are always coerced to strings. Duplicate keys are not allowed. javascript const params = new URLSearchParams({ user: 'ana', course: ['math', 'chem', 'phys'] }); console.log(params.toString()); Output: user=ana&course=math%2Cchem%2Cphys new URLSearchParams(iterable): Accepts an iterable object having a collection of key-value pairs to instantiate a new URLSearchParams object. Iterable can be any iterable object. Since URLSearchParams is iterable, an iterable object can be another URLSearchParams, where the constructor will create a clone of the provided URLSearchParams. Duplicate keys are allowed. JavaScript // Using a Map object as it is iterable const map = new Map(); map.set('West Bengal', 'Kolkata'); map.set('Karnataka', 'Bengaluru'); params = new URLSearchParams(map); console.log(params.toString()); Output: West+Bengal=Kolkata&Karnataka=Bengaluru Accessing the URL query: urlSearchParams.get(name): Returns the value of the first name-value pair that matches with the argument passed. If no such pair exists, null is returned. javascript const myURL = new URL( 'https://example.org/?abc=123&abc=526'); console.log(myURL.searchParams.get('abc')); Output: 123 urlSearchParams.getAll(name): Returns all the value of the name-value pair that matches with the argument passed. If no such pair exists, null is returned. javascript const myURL = new URL( 'https://example.org/?abc=123&abc=526'); console.log(myURL.searchParams.getAll('abc')); Output: [ '123', '526' ] urlSearchParams.has(name): Returns true if the argument passed matches with any existing name of the name-value pair else returns false. javascript const myURL = new URL( 'https://example.org/?abc=123&xyz=526'); console.log(myURL.searchParams.has('abc')); console.log(myURL.searchParams.has('pqr')); Output: true false Manipulating the URL query: urlSearchParams.set(name, value): Sets the value in the URLSearchParams object associated with name to the specified value. If more than one name-value pairs exists, whose names are same as the 'name' argument, then the only value of first matching pair is changed, rest all are removed. javascript const params = new URLSearchParams( 'abc=123&xyz=526&abc=258'); console.log(params.toString()); params.set('abc', 'opq'); console.log(params.toString()); Output: abc=123&xyz=526&abc=258 abc=opq&xyz=526 urlSearchParams.append(name, value): Appends a new name-value pair to the existing URLSearchParams query. javascript const params = new URLSearchParams('xyz=123'); params.append('foo', '789'); params.append('xyz', 'zoo'); params.append('foo', 'def'); console.log(params.toString()); Output: xyz=123&foo=789&xyz=zoo&foo=def urlSearchParams.delete(name): Removes all name-value pairs whose name is same as 'name' argument. javascript const params = new URLSearchParams( 'xyz=123&foo=789&xyz=zoo&foo=def'); console.log(params.toString()); params.delete('foo'); console.log(params.toString()); Output: xyz=123&foo=789&xyz=zoo&foo=def xyz=123&xyz=zoo urlSearchParams.sort(): Sorts the existing name-value pairs in-place by their names using a stable sorting algorithm. javascript const params = new URLSearchParams( 'query=node&type=search&abc=programs'); params.sort(); console.log(params.toString()); Output: abc=programs&query=node&type=search urlSearchParams.toString(): Returns the URL search parameters as a string, with characters percent-encoded wherever necessary. javascript const params = new URLSearchParams( 'query=node&type=search&passwd[]=3456'); console.log(params.toString()); Output: query=node&type=search&passwd%5B%5D=3456 Iterating the URL query: urlSearchParams.entries(): Returns an iterator over the entry set of the param object. javascript const params = new URLSearchParams( 'query=node&type=search&passwd=3456'); for(var pair of params.entries()) { console.log(pair[0]+ '-->'+ pair[1]); } Output: query-->node type-->search passwd-->3456 urlSearchParams.keys(): Returns an iterator over the key set of the param object. javascript const params = new URLSearchParams( 'query=node&type=search&passwd=3456'); for(var key of params.keys()) { console.log(key); } Output: query type passwd urlSearchParams.values(): Returns an iterator over the value set of the param object. javascript const params = new URLSearchParams( 'query=node&type=search&passwd=3456'); for(var value of params.values()) { console.log(value); } Output: node search 3456 urlSearchParams.forEach(fn[, arg]): fn is a function invoked for each name-value pair in the query and arg is an object to be used when 'fn' is called. It iterates over each name-value pair in the query and invokes the function. javascript const myURL = new URL( 'https://example.com/?a=b&c=d&d=z'); myURL.searchParams.forEach( (value, name, searchParams) => { console.log(name, value, myURL.searchParams === searchParams); }); Output: a b true c d true d z true urlSearchParams[Symbol.iterator](): javascript const params=new URLSearchParams( 'firstname=john&lastname=beck&gender=male'); for (const [name, value] of params) { console.log(name, value); } Output: firstname john lastname beck gender male Create Quiz Comment S Shreyasi_Chakraborty Follow 6 Improve S Shreyasi_Chakraborty Follow 6 Improve Article Tags : Web Technologies Node.js Node.js-Basics Node.js-Globals Explore Introduction & Installation NodeJS Introduction3 min readNode.js Roadmap: A Complete Guide6 min readHow to Install Node.js on Linux6 min readHow to Install Node.js on Windows5 min readHow to Install NodeJS on MacOS6 min readNode.js vs Browser - Top Differences That Every Developer Should Know6 min readNodeJS REPL (READ, EVAL, PRINT, LOOP)4 min readExplain V8 engine in Node.js7 min readNode.js Web Application Architecture3 min readNodeJS Event Loop5 min readNode.js Modules , Buffer & StreamsNodeJS Modules5 min readWhat are Buffers in Node.js ?4 min readNode.js Streams4 min readNode.js Asynchronous ProgrammingAsync Await in Node.js3 min readPromises in NodeJS7 min readHow to Handle Errors in Node.js ?4 min readException Handling in Node.js3 min readNode.js NPMNodeJS NPM6 min readSteps to Create and Publish NPM packages7 min readIntroduction to NPM scripts2 min readNode.js package.json4 min readWhat is package-lock.json ?3 min readNode.js Deployments & CommunicationNode Debugging2 min readHow to Perform Testing in Node.js ?2 min readUnit Testing of Node.js Application5 min readNODE_ENV Variables and How to Use Them ?2 min readDifference Between Development and Production in Node.js3 min readBest Security Practices in Node.js4 min readDeploying Node.js Applications5 min readHow to Build a Microservices Architecture with NodeJS3 min readNode.js with WebAssembly3 min readResources & ToolsNode.js Web Server6 min readNode Exercises, Practice Questions and Solutions4 min readNode.js Projects9 min readNodeJS Interview Questions and Answers15+ min read Like