Node.js URLsearchParams API

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:

  1. new URLSearchParams(): No argument constructor instantiates a new empty URLSearchParams object.
  2. new URLSearchParams(string): Accepts a string as an argument to instantiate a new URLSearchParams object.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    var params = new URLSearchParams('user=abc&q=xyz');
    console.log(params.get('user'));
    console.log(params.get('q'));

    chevron_right

    
    

    Output:

    abc
    xyz
  3. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const params = new URLSearchParams({
      user: 'ana',
      course: ['math', 'chem', 'phys']
    });
    console.log(params.toString());

    chevron_right

    
    

    Output:



    user=ana&course=math%2Cchem%2Cphys
  4. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const myURL = new URL(
      
    console.log(myURL.searchParams.get('abc'));

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const myURL = new URL(
    console.log(myURL.searchParams.getAll('abc'));

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const myURL = new URL(
    console.log(myURL.searchParams.has('abc'));
    console.log(myURL.searchParams.has('pqr'));

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const params = new URLSearchParams(
        'abc=123&xyz=526&abc=258');
    console.log(params.toString());
    params.set('abc', 'opq');
    console.log(params.toString());

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const params = new URLSearchParams('xyz=123');
    params.append('foo', '789');
    params.append('xyz', 'zoo');
    params.append('foo', 'def');
    console.log(params.toString());

    chevron_right

    
    

    Output:



    xyz=123&foo=789&xyz=zoo&foo=def
  • urlSearchParams.delete(name): Removes all name-value pairs whose name is same as ‘name’ argument.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const params = new URLSearchParams(
      'xyz=123&foo=789&xyz=zoo&foo=def');
    console.log(params.toString());
    params.delete('foo');
    console.log(params.toString());

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const params = new URLSearchParams(
      'query=node&type=search&abc=programs');
    params.sort();
    console.log(params.toString());

    chevron_right

    
    

    Output:

    abc=programs&query=node&type=search
  • urlSearchParams.toString(): Returns the URL search parameters as a string, with characters percent-encoded wherever necessary.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const params = new URLSearchParams(
      'query=node&type=search&passwd[]=3456');
    console.log(params.toString());

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const params = new URLSearchParams(
        'query=node&type=search&passwd=3456');
    for(var pair of params.entries()) {
       console.log(pair[0]+ '-->'+ pair[1]); 
    }

    chevron_right

    
    

    Output:

    query-->node
    type-->search
    passwd-->3456
  • urlSearchParams.keys(): Returns an iterator over the key set of the param object.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const params = new URLSearchParams(
        'query=node&type=search&passwd=3456');
    for(var key of params.keys()) {
       console.log(key); 
    }

    chevron_right

    
    

    Output:

    query
    type
    passwd
  • urlSearchParams.values(): Returns an iterator over the value set of the param object.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const params = new URLSearchParams(
        'query=node&type=search&passwd=3456');
    for(var value of params.values()) {
       console.log(value); 
    }

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const myURL = new URL(
    myURL.searchParams.forEach(
      (value, name, searchParams) => {
    console.log(name, value, 
      myURL.searchParams === searchParams);
    });

    chevron_right

    
    

    Output:

    a b true
    c d true
    d z true
  • urlSearchParams[Symbol.iterator]():
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const params=new URLSearchParams(
        'firstname=john&lastname=beck&gender=male');
    for (const [name, value] of params) {
      console.log(name, value);
    }

    chevron_right

    
    

    Output:

    firstname john
    lastname beck
    gender male
    



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.