Node.js – util.format() method


The util.format() method returns a formatted string that will use the first argument as a printf like format string. This format can also contain zero or more format specifiers. These specifiers can be replaced with the converted value from the corresponding argument.

Following are some of the formatted specifiers:

  • %s − String will be used to convert all values except bigInt, object and -0.
  • %d − In this case, number will be used to convert all values except BigInt and symbo
  • %iparseInt(value, 10) will be used for all values except BigInt and symbol.
  • %fparseFloat(value) will be used for all values except Symbol
  • %j − This format type represents JSON
  • %o − This will represent an object with generic JavaScript object formatting.
  • %c − This specifier will be ignored and skip any CSS that is passed in.
  • %% − This does not consume an argument.

Syntax

util.format(format, [args])

Parameters

The parameters are defined below:

  • format − This input parameter will take input for the specifiers which is like the printf format string.
  • args − This is the list of arguments that is passed.

Example 1

Create a file "format.js" and copy the following code snippet. After creating the file, use the command "node format.js" to run this code.

 Live Demo

// Node.js util.format() demo Example

// Importing the util module
const util = require('util');

function fun1() {
   var val1 = util.format('%s:%s:%s', 'tutorialsPoint');

   var val2 = util.format('%s:%s','a', 'b', 'c', 'd');

   var val3 = util.format(10, 20, 30);

   var val4 = util.format('%% : %s : %d');

   var val5 = util.format('%% : %s', 786);

   console.log(val1, '
', val2, '
',val3, '
', val4, '
', val5); } // Function call fun1();

Output

C:\home
ode>> node format.js tutorialsPoint: :a:b c d 10 20 30 %% : %s : %d % : 786

Example 2

 Live Demo

// Node.js program to demonstrate
// the util.format() method

// Import the util module
const util = require('util');

// Passing -0 to %s
console.log("1.", util.format(
'%%: %s', 'abc', 'def', -0));

// Passing a bigInt to string specifier
console.log("2.", util.format('%s',
'abc', 123456789009876543213456789));

// Passing an object with null identifier
console.log("3.", util.format('%s',
'abc', Object.create(null,
{ [Symbol.toStringTag]:
{ value: 'def' } })));

// Passing string to Number specifier
console.log("4.", util.format('%d',
'abc', 94303685));

// Passing symbol and number to int formatter
console.log("5.", util.format(
'%i', '2020 year 2021, ', 'He was 40,'
, '10.33, ', '10, ', 10));

// Passing string and Numbers
// to parseFloat specifier
console.log("6.>", util.format('%f',
'94321321321.564000 year 6546',
'abc', 943036854775807));

// Passing a class with the below value to object formatter
console.log("7. ", util.format('%o:%d',
   class Foo { get [Symbol.toStringTag]()
      { return 'abc'; } },
      'abc',
      12345678900987
));

Output

C:\home
ode>> node format.js 1. %: abc def 0 2. abc 1.2345678900987654e+26 3. abc [Object: null prototype] [def] {} 4. NaN 94303685 5. 2020 He was 40, 10.33, 10, 10 6.> 94321321321.564 abc 943036854775807 7. { [Function: Foo]    [length]: 0,    [prototype]:    Foo [abc] {       [constructor]: [Circular],       [Symbol(Symbol.toStringTag)]: [Getter] },     [name]: 'Foo' }:NaN 12345678900987

Updated on: 16-Aug-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements