Node.js util.formatWithOptions() Method22 Feb 2025 | 4 min read In this article, we will discuss the util.formatWithOptions() method in Node.js with its syntax, parameters, and examples. What is the util.formatWithOptions() Method in Node.js?The util.formatWithOptions() method is a built-in API within the util module. It was first introduced in Node.js v10.0.0. It formats strings similar to printf in other programming languages. The first argument serves as a template for the formatted string returned by this method. These options are similar to those used with util.inspect(), providing greater control and flexibility when formatting strings, especially when including objects. The formatted string may include zero or more format specifiers where appropriate argument values have been converted and inserted in place of the specifiers. The util.formatWithOptions() function is a powerful debugging tool for developers because it can provide informative and readable debug output. Since it is a synchronous method, it ensures that formatting and inspection are complete before the string is returned. Syntax:It has the following syntax: Parameters:
Return Value <string>:The type's formatted string is returned. Example:Let us take an example to illustrate the Node.js util.formatWithOptions() method. Output:
1 => %: Peter John -0
2 => % Peter John Joseph
3 => Peter 94357321361723374516854775807n
4 => Peter [Object: null prototype] [john] {}
5 => NaN 94751625
6 => 2023 Alex was 40 12.44 10 10
7 => 947583221.567 Peter 789879654065464
8 => {"name":"Joseph","age":25,"city":"Hyderabad"} Peter 78987965465464
9 => [class Bar] {
[length]: 0,
[prototype]: Bar { [constructor]: [Circular] },
[name]: 'Bar'
} Peter 78987967415464
10 => [class Foo] {
[length]: 0,
[prototype]: Foo {
[constructor]: [Circular],
[Symbol(Symbol.toStringTag)]: [Getter]
},
[name]: 'Foo'
}:NaN 78978425464964
Explanation:By importing the util module and utilizing a variety of format specifiers to format strings with a range of values, this Node.js program demonstrates how to use the util.formatWithOptions() method. Instances of BigInt, objects with null prototypes, passing multiple values, strings to number specifiers, JSON objects, and class instances are among the cases covered by the examples. The console.log() method is used to display the formatted output, and the readability of the output is improved by configuring settings like colors and depth. Each example demonstrates how multiple format specifiers and parameters may be used to efficiently format and examine different types of data. Next TopicNodejs-util-types-isnumberobject-method |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India