Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js util.formatWithOptions() Method

  • Last Updated : 23 Aug, 2021

The util.formatWithOptions() (Added in v10.0.0) 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. It is somewhat identical to util.format() method, the exception in this method is that it takes an extra argument i.e. inspectOptions that specify options that are passed along to util.inspect() method
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 is a synchronous method. 

Syntax:

util.formatWithOptions(inspectOptions, format[, ...args])

To use this function we have to import the util module:

const util=require("util");   

Parameters: This method accepts three parameters as mentioned above and described below:

  • inspectOptions <object> : It accepts the functions, objects, callbacks, JSON format data, etc.
  • format<string>: It consists of specifiers of <string> type, which is like printf format string.
  • args<string>: It accepts the <string> type list or array of arguments.

Return Value<string>: It returns the formatted string of <string> type.

Example 1: Filename: index.js

javascript




// Node.js to demonstrate the
// util.formatWithOptions() method
 
// Import the util module
const util = require('util');
 
function fun1() {
 
    // Returns: See object: {alfa:42}
    var val0 = util.formatWithOptions(
        { depth:0, colors: true },
        'See object %O', { alfa: 42 });
     
    // Returns: 'abc:%s'
    var val1 = util.formatWithOptions(
        { colors: true, showProxy : true },
        '%s:%s:%s', 'abc');
 
    // Returns: 'abc:def ghi'
    var val2 = util.formatWithOptions(
        { showHidden: true, colors: true },
        '%s:%s', 'abc', 'def', 'ghi', 'jkl');
 
    // Returns: '10 20 30'
    var val3 = util.formatWithOptions(
        { breakLength: 80, colors: true },
        10, 20, 30);
 
    // Returns: '% : 567'
    var val5 = util.formatWithOptions(
        { compact: true }, '%% : %s', 567);
      
    // Printing all values
    console.log(val0, '\n', val1, '\n',
        val2, '\n', val3, '\n', val5);
}
 
// Function call
fun1();

Output:

See object { alfa: 42 } 
abc:%s:%s 
abc:def ghi jkl        
10 20 30 
% : 567

Example 2: Filename: index.js

javascript




// Node.js program to demonstrate the
// util.formatWithOptions() method
 
// Import the util module
const util = require('util');
 
// Passing multiple values and
// -0 on string specifier
console.log("1 => ", util.formatWithOptions(
    { colors: true },
    '%%: %s', 'alfa', 'beta', -0));
 
// Passing multiple values
console.log("2 => ", util.formatWithOptions(
    { colors: true },
    '%%', 'alfa', 'beta', 'gamma'));
 
// Passing bigInt to string specifier
console.log("3 => ", util.formatWithOptions(
    { colors: true }, '%s',
    'alfa', 94321321321223372036854775807));
 
// Creating and passing Object along
// with null prototype and a variable
console.log("4 => ", util.formatWithOptions(
    {
        showHidden: false, depth: 0,
        colors: true
    }, '%s', 'alfa', Object.create(null,
    { [Symbol.toStringTag]: { value: 'beta' } })));
 
// Passing string to Number specifier
console.log("5 => ", util.formatWithOptions(
    { colors: true }, '%d', 'alfa',
    94303685));
 
// Passing Symbol and Number to parseInt specifier
console.log("6 => ", util.formatWithOptions(
    { showHidden: false, colors: true },
    '%i', '2020 year 2021, ', 'He was 40, ',
    '10.33, ', '10, ', 10));
 
// Passing string and Numbers to
// parseFloat specifier
console.log("7 => ", util.formatWithOptions(
    { colors: true }, '%f',
    '94321321321.564000 year 6546',
    'alfa', 78987965465464));
 
// Passing JSON string and Number
// to JSON specifier
console.log("8 => ", util.formatWithOptions(
    { depth: 0, colors: true }, '%j',
    '{ "name":"Chotu Mishra", "age":23, "city":"Kanpur" }',
    'alfa', 78987965465464));
 
// Passing class, string, and Number
// to object specifier
console.log("9 => ", util.formatWithOptions(
    { colors: true }, '%o',
    class Bar { }, 'alfa', 78987965465464));
 
// Passing class, string, and Number
// to Object specifier
console.log("10 => ", util.formatWithOptions(
    { depth: 0, colors: true }, '%o:%d',
    class Foo { get [Symbol.toStringTag]()
        { return 'alfa'; } }, 'alfa',
    78987965465464));

Run index.js file using the following command:

node index.js

Output:

1 =>  %: alfa beta -0
2 =>  % alfa beta gamma
3 =>  alfa 9.432132132122338e+28
4 =>  alfa [Object: null prototype] [beta] {}
5 =>  NaN 94303685
6 =>  2020 He was 40,  10.33,  10,  10
7 =>  94321321321.564 alfa 78987965465464
8 =>  "{ \"name\":\"Chotu Mishra\", \"age\":23, 
      \"city\":\"Kanpur\" }" alfa 78987965465464
9 =>  [Function: Bar] {
  [length]: 0,
  [prototype]: Bar { [constructor]: [Circular] },
  [name]: 'Bar'
} alfa 78987965465464
10 =>  [Function: Foo] {
  [length]: 0,
  [prototype]: Foo {
    [constructor]: [Circular],
    [Symbol(Symbol.toStringTag)]: [Getter]
  },
  [name]: 'Foo'
}:NaN 78987965465464

Reference: https://nodejs.org/api/util.html#util_util_formatwithoptions_inspectoptions_format_args


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!