

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
URLSearchParams.set & append() in Node
Introduction to set()
This function can be used to set the value of the name argument passed with the new value passed. If multiple name-value pair exists, only one name-value pair will be set and all the remaining pairs will be removed as shown in the example below.
Syntax
URLSearchParams.set(name, value);
Parameters
The inputs are name and a value. The name is used to find the value that needs to be updated with the new value given in the argument. New value is not set if the name paramter does not exist in the URL.
Example
// Defining the URL as a constant const params = new URLSearchParams( 'firstName=John&firstName=Mark&lastName=Chan'); console.log(params.toString()); // Setting the name-value pair params.set('firstName', 'Jackie'); // Printing all the params that match value -> 'firstName' console.log(params.toString());
Output
firstName=John&firstName=Mark&lastName=Chan firstName=Jackie&lastName=Chan
Example (When the argument value is not present)
// Defining the URL as a constant const params = new URLSearchParams( 'firstName=John&firstName=Mark&lastName=Chan'); console.log(params.toString()); // Setting the name-value pair params.set('midName', 'abc'); // Printing all the params that match value -> 'firstName' console.log(params.toString());
Output
firstName=John&firstName=Mark& lastName=Chan firstName=John&firstName=Mark&lastName=Chan
Introduction to append()
This function appends a new name-value pair to the existing URL. The name-value pair is appended in the last.
Syntax
URLSearchParams.append(name, value);
Parameters
The name-value pair that needs to appended in the URL.
Example
// Defining the URL as a constant const params = new URLSearchParams( 'firstName=Jackie'); // Appending a new name-value pair params.append('lastName', 'Chan'); // Printing the new URL console.log(params.toString());
Output
firstName=Jackie&lastName=Chan
Example
// Defining the URL as a constant const myURL = new URL( 'https://example.org/?empId=2'); params.append('empName', 'John'); // Printing all the params that match value -> 'Id' console.log(url);
Output
https://example.org/?empId=2&empName=John
- Related Questions & Answers
- URLSearchParams entries & forEach in Node
- URLSearchParams sort & toString() in Node
- URLSearchParams values & keys() in Node
- Introduction to URLSearchParams API in Node.js
- append() and extend() in Python
- append() and extend() in Python program
- How to set font to text node in JavaFX?
- First and last child node of a specific node in JavaScript?
- Append at front and remove from rear in Python
- SignUp form using Node and MongoDB
- What is the difference between append() and appendTo() in jQuery?
- Append to StringBuilder in C#
- Preappend or Append radio buttons and checkboxes with Bootstrap
- Append Dictionary Keys and Values (In order ) in dictionary using Python
- Maximum Difference Between Node and Ancestor in C++
Advertisements