Categories
JavaScript Answers

How to print formatted console output with Node.js?

Spread the love

To print formatted console output with Node.js, we use the sprintf-js module.

For instance, we write

const { sprintf } = require("sprintf-js");

console.log(sprintf("Space Padded => %10.2f", 123.4567));

to call sprintf with the format string and the value we want to print.

We round the number to 2 decimal places with %10.2f and return the formatted string which we print with console.log.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *