• Node.js Video Tutorials

NodeJS - urlSearchParams.get() Method



The NodeJS urlSearchParams.get() method of URLSearchParams class retrieves the value from a specified name in the query string.

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

To get a better understanding of this method, let’s look into a real-life example. Consider this URL (‘https://www.youtube.com/watch?v=towpH780PT0’), where 'v=towpH780PT0' is known as the query segment. In this query, (v) is the name, and (towpH780PT0) is the value. Together it forms a name-value pair.

Syntax

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

URLSearchParams.get(name)

Parameters

  • name: This specifies the name of the parameter to return.

Return Value

This method returns a string value of the given name passed as a parameter.

  • If there are multiple name-value pairs with the given name, this method returns the value in the first pair.

  • If there is no pair whose name is name, null is returned.

Example

In the following example, we are trying to get the value of the key named ‘two’ in the query string.

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

const MyUrl = new URL('https://www.tutorialspoint.com?one=1&two=2&three=3');
console.log("URL: ", MyUrl.href);

const Params = new URLSearchParams('one=1&two=2&three=3');
console.log("Query string: " + Params);

console.log("Trying to get the value of key 'two'.....");
console.log("The value is: " + Params.get("two"));

Output

After executing the above program, the NodeJS get() method returns the value of the key ‘two’.

URL:  https://www.tutorialspoint.com/?one=1&two=2&three=3
Query string: one=1&two=2&three=3
Trying to get the value of key 'two'.....
The value is: 2

Example

If the name to be searched is present multiple times in the query string, the get() method returns the value of its first occurrence.

In the program below, we are trying to get the value of the name ‘two’ in the query string.

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

const MyUrl = new URL('https://www.tutorialspoint.com?two=1&two=2&two=3');
console.log("URL: ", MyUrl.href);

const Params = new URLSearchParams('two=1&two=2&two=3');
console.log("Query string: " + Params);

console.log("Trying to get the value of the first key 'two'.....");
console.log("The value is: " + Params.get("two"));

Output

As we can see in the output, the get() method returns the value of the first occurrence.

URL:  https://www.tutorialspoint.com/?two=1&two=2&two=3
Query string: two=1&two=2&two=3
Trying to get the value of the first key 'two'.....
The value is: 1

Example

If the name to be searched is not present in the query string, the get() method returns null.

In the example below, we are trying to get the value of a name that is not present in the query string.

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

const MyUrl = new URL('https://www.tutorialspoint.com?HTML=499&NODEJS=999&CSS=699');
console.log("URL: ", MyUrl.href);

const Params = new URLSearchParams('HTML=499&NODEJS=999&CSS=699');
console.log("Query string: " + Params);

console.log("Trying to get the value of the first key 'JavaScript'.....");
console.log("The value is: " + Params.get("JavaScript"));

Output

As we can see in the output, the get() method returns null.

URL:  https://www.tutorialspoint.com/?HTML=499&NODEJS=999&CSS=699
Query string: HTML=499&NODEJS=999&CSS=699
Trying to get the value of the first key 'JavaScript'.....
The value is: null
nodejs_url_module.htm
Advertisements