• Node.js Video Tutorials

NodeJS URLSearchParams.append() Method



The NodeJS urlSearchParams.append() method of URLSearchParams class appends a specified name/value pair as a new search parameter in the query string. If the same name is appended multiple times with different values, these will be added to the query string multiple times for each value.

The query portion of the URL can be read and written using the URLSearchParams API. This class is also available on the global object.

Syntax

Following is the syntax of the NodeJS URLSearchParams.append() method

URLSearchParams.append(name, value)

Parameters

This method accepts only two parameters. The same are described below.

  • name: This parameter contains the name that will be appended.

  • value: This parameter contains the value that will be appended.

Return Value

This method appends a new name-value pair to the query string.

Example

If we pass a key/value pair to the NodeJS urlSearchParams.append() method, it will add them at the end of the query string.

In the following example, we are trying to append a new key-value pair to the input query string.

const url = require('node:url');

let Myurl = new URL('https://www.tutorialspoint.com?HTML=10&CSS=20');
let params = new URLSearchParams('HTML=10&CSS=20');

//Adds 'JavaScript' with value 30.
params.append('JavaScript', 30);
//The final Query string is: 'HTML=10&CSS=20&JavaScript=30'
console.log(params.toString());

Output

As we can see in the output below, the NodeJS urlSearchParams.append() method adds the specified key-value pair to the end of the query string.

HTML=10&CSS=20&JavaScript=30

Example

If we pass the same key multiple times with different values to the append() method, they will be added in the query string multiple times for each value.

In the following example, we are trying to add the same key multiple with different values to the query string.

const url = require('node:url');

let Myurl = new URL('https://www.tutorialspoint.com?HTML=10&CSS=20');
let params = new URLSearchParams('HTML=10&CSS=20');

//Adds 'JavaScript' with value 30, 40, and 50
params.append('JavaScript', 30);
params.append('JavaScript', 40);
params.append('JavaScript', 50);

//The final Query string is : 'HTML=10&CSS=20&JavaScript=30&JavaScript=40&JavaScript=50'
console.log(params.toString());

Output

As we can see in the output below, the key-value pairs are added to the query string.

HTML=10&CSS=20&JavaScript=30&JavaScript=40&JavaScript=50

Example

We can also append a key/value pair to the query string by using the set() method. This method also adds the key/value pair at the end of the query string.

In the program below, we are trying to append key-value pairs by using the set() method.

const url = require('node:url');

let Myurl = new URL('https://www.tutorialspoint.com?HTML=10&CSS=20');
let params = new URLSearchParams('HTML=10&CSS=20');

//Adds 'JavaScript' with value 30.
params.set('JavaScript', 30);
//Adds 'Node' with value 90.
params.set('Node', 90)
//The final Query string is: 'HTML=10&CSS=20&JavaScript=30&Node=90'
console.log(params.toString());

Output

As we can see in the output below, the set() method added the key-value pairs at the end of the query string.

HTML=10&CSS=20&JavaScript=30&Node=90
nodejs_url_module.htm
Advertisements