What is the querystring.escape() method in Node.js?

The querystring.escape() method in Node.js performs URLUniform Resource Locator percent-encoding on a given string. The querystring.escape method iterates over a string and places the % encoding to convert the given string into a URL query.

The process is illustrated below:

Note: The querystring.escape() method is rarely used directly. Instead, it is used implicitly by the querystring.stringify() method. You can read up on the other querystring methods in the official documentation.

The querystring.escape() method encodes all characters except the following:

A-Z a-z 0-9 - _ . ! ~ * ’ ( )

To use the querystring.escape() method, you will need to import the querystring module into the program, as shown below:

const querystring = require("querystring")

Parameters

The querystring.escape() method takes a single mandatory parameter that represents the string to be encoded.

Return value

The querystring.escape() method returns the percent-encoded query string formed from the provided parameter.

Example

The code below shows how the querystring.escape() method works in Node.js:

const querystring = require("querystring")
// initializing string
let normal_string = "query strings with educative = fun learning?"
// performing encoding
let encoded_string = querystring.escape(normal_string)
// output results
console.log("Original string: ", normal_string)
console.log("Encoded query string: ", encoded_string)

Explanation

First, the code initializes a string object normal_string. The querystring.escape() method in line 77 proceeds to add the percentage encoding characters, e.g., %20, %3D, and %3F%, in place of white spaces, the = operator, and the ? symbol, respectively. As a result, the encoded URL query string is created.

Note: You can find a comprehensive list of characters and their corresponding encodings here.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved