- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
NodeJS - urlSearchParams.set() Method
The NodeJS urlSearchParams.set() method of URLSearchParams class is used to set the name-value pairs to the query string.
The URLSearchParams API provides methods that give access to read and write the query of a URL. This class is also available on the global object.
Syntax
Following is the syntax of the NodeJS URLSearchParams.set() method
URLSearchParams.set(name, value)
Parameters
This method accepts two parameters. The same are described below.
name: This specifies the name of the parameter to set.
value: This specifies the value of the parameter to set.
Return Value
This method returns undefined.
Example
If we pass values to the ‘name’ and ‘value’ parameters of the NodeJS urlSearchParams.set() method, it will set those values as a name-value pair to the query string.
In the following example, we are trying to set a few name-value pairs to an empty query string.
const url = require('node:url'); let params = new URLSearchParams(''); console.log('Query string: ' + params); console.log('Setting name-value pairs to the query string.....'); params.set('txt', 10); params.set('pdf', 20); params.set('dcmnt', 30); params.set('word', 40); console.log("Query string: " + params.toString());
Output
On executing the above program, it will generate the following output
Query string: Setting name-value pairs to the query string..... Query string: txt=10&pdf=20&dcmnt=30&word=40
Example
If there are any pre-existing name-value pairs in the query string are matching the name to be set by the NodeJS set() method, it will remove all those matching name-value pairs and sets the newly added pair.
In the following example, we are trying to add a name-value pair that matches the existing name-value pair in the query string.
const url = require('node:url'); let params = new URLSearchParams('txt=10&pdf=20&dcmnt=30&word=40'); console.log('Query string: ' + params); console.log('Setting name-value pairs to the query string.....'); params.set('txt', 15); console.log("Query string: " + params.toString());
Output
On executing the above program, it will generate the following output
Query string: txt=10&pdf=20&dcmnt=30&word=40 Setting name-value pairs to the query string..... Query string: txt=15&pdf=20&dcmnt=30&word=40
Example
If the values to be set by the set() method contains special characters, they will be percent-encoded and will be set to the query string.
const url = require('node:url'); let params = new URLSearchParams('txt=10&pdf=20&dcmnt=30&word=40'); console.log('Query string: ' + params); console.log('Setting name-value pairs to the query string.....'); params.set('t`x`t', 15); console.log("Query string: " + params.toString());
Output
On executing the above program, it will generate the following output
Query string: txt=10&pdf=20&dcmnt=30&word=40 Setting name-value pairs to the query string..... Query string: txt=10&pdf=20&dcmnt=30&word=40&t%60x%60t=15
To Continue Learning Please Login
Login with Google