![]() |
VOOZH | about |
The util.format() (Added in v0.5.3) method is an inbuilt application programming interface of the util module which is like printf format string and returns a formatted string using the first argument. The formatted string contains zero or more format specifiers in which the corresponding argument value is converted and replaced. It is used as a debugging tool in a synchronous method hence, it can have a significant performance overhead that could block the event loop. It is recommended not to use this function in a hot code path.
Syntax:
util.format(format[, ...args])
Parameters: This method accepts two parameters as mentioned above and described below:
Supported specifiers are:
Return Value: It returns the formatted string of <string> type.
Example 1: In this example, we will see the use of the util.format().
index.js
Run the index.js file using the following command:
node index.js
Output:
abc: :abc:def ghi jkl 10 20 30 %% : %s : %d % : 567
Example 2: In this example, we will see the use of the util.format().
index.js
Run the index.js file using the following command:
node index.js
Output:
1.> %: abc def -0
2.> % abc def ghi
3.> abc 9.432132132122338e+28
4.> abc [Object: null prototype] [def] {}
5.> NaN 94303685
6.> 2020 He was 40, 10.33, 10, 10
7.> 94321321321.564 abc 943036854775807
8.> "{ \"name\":\"John\", \"age\":31,
\"city\":\"New York\" }" abc 943036854775807
9.> <ref *1> [Function: Bar] {
[length]: 0,
[prototype]: Bar { [constructor]: [Circular *1] },
[name]: 'Bar'
} abc 943036854775807
10.> <ref *1> [Function: Foo] {
[length]: 0,
[prototype]: Foo {
[constructor]: [Circular *1],
[Symbol(Symbol.toStringTag)]: [Getter]
},
[name]: 'Foo'
}:NaN 943036854775807
11.> randomClass {}
Conditions:
Reference: https://nodejs.org/api/util.html#util_util_format_format_args