Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is querystring.stringify in Node.js?

Amaaz Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The querystring.stringify function is used to create a URL query string from an object.

The object contains a list of key-value pairs.

We can find the querystring.stringify function in the Query String module of Node.js.

The querystring.stringify function can be accessed in your program by:

const querystring = require("querystring");

Parameters

querystring.stringify( obj[, sep[, eq[, options]]] )

The function takes up to four parameters, as described below :

  • obj is the object that will be converted into a URL query string.
  • sep is the substring used to delimit key and value pairs in the query string.

Default value for sep is &.

  • eq is the substring used to delimit key and value pairs in the query string.

Default value for eq is =.

  • options is an object and allows us to change the function behavior with respect to character encoding. The options object can have the following argument:
    • decodeURIComponent: This is the function to use when converting URL-unsafe characters to percent-encoding in the query string.

The querystring.unescape method is used as a default for decodeURIComponent.

Apart from obj, all other parameters are optional paramters.

Return value

The function returns the newly created string with suitable delimiters.

Example

// Import the querystring module
const querystring = require("querystring");
// defining the object which will be stringified
var obj = {username: "educative_user", role:["programmer", "manager"]}
// calling the stringify function with default delimiters
console.log("Generated String: ",querystring.stringify(obj))

In the above example, an object is converted into a string using the querystring.stringify function.

RELATED TAGS

node.js

CONTRIBUTOR

Amaaz Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring