Open In App

Node.js querystring.encode() Function

The querystring.encode() method is used to produce a URL query string from the given object that contains the key-value pairs. The method iterates through the object’s own properties to generate the query string.

It can serialize a single or an array of strings, numbers, and boolean. Any other types of values are coerced to empty strings.



During serializing, the UTF-8 encoding format is used to encode any character that requires percent-encoding. To encode using an alternative character encoding, the encodeURIComponent option has to be specified.

Syntax:



querystring.encode( obj, sep, eq, options )

Parameters: This function accepts four parameters as mentioned above and described below:

Return Value: It returns a String that contains the URL query produced from the given object.

Example 1:




const querystring = require('querystring');
  
let obj = {
    user: "pratik"
    isMale: true
    role: ["admin", "editor", "manager"], 
}
  
let output = querystring.encode(obj);
  
console.log("Output: ", output);

Output:

Example 2:




const querystring = require('querystring');
  
let obj = {
    user: "pratik"
    isMale: true
    role: ["admin", "editor", "manager"], 
}
  
let output = querystring.encode(obj, '/', '->');
  
console.log("Output: ", output);

Output:

Reference:https://nodejs.org/api/querystring.html#querystring_querystring_encode


Article Tags :