HOWTO · JavaScript

JavaScript Equivalent to Printf or String.Format

This tutorial describes how to replicate the functionality of printf() in JavaScript. Explore various methods like template literals, Intl.NumberFormat, and the sprintf-js library to format strings effectively. Enhance your JavaScript skills with practical examples and detailed explanations for each approach.

On this page

JavaScript does not have a built-in printf or String.Format function. Use a template literal to insert values or expressions into ordinary text, Intl.NumberFormat to format numbers for a locale, and sprintf-js when you specifically need printf-style format specifiers. console.log displays values in a console; it does not create a formatted string. The template literal and Intl.NumberFormat examples below were executed; the inline sprintf-js example was not included in the supplied runtime cases.

Using Template Literals

Template literals are the simplest choice when you are building a message from known values. Wrap the text in backticks and put each variable or expression inside ${...}. The example builds a greeting from a name and a calculation.

const name = "Alice";
const age = 30;
const greeting = `Hello, my name is ${name} and I am ${age} years old.`;
console.log(greeting);
Hello, my name is Alice and I am 30 years old.

This approach also supports multiline strings and expressions such as ${price * quantity}. It keeps the resulting string visible in the source and needs no formatter dependency. Use console.log(greeting) when you want to inspect or display the value during development; use the greeting string itself when passing data to another function or saving it.

Using the Intl.NumberFormat Object

For currency, percentages, and other numeric values, use Intl.NumberFormat. It applies grouping separators, decimal conventions, and currency placement for the locale you select. It formats a number, rather than arbitrary text.

const amount = 123456.789;
const formattedAmount = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD"
}).format(amount);
console.log(formattedAmount);
$123,456.79

Choose the locale and currency from the user’s context instead of assuming every reader uses US dollars. For example, de-DE with EUR uses German conventions for a euro amount. The exact punctuation and symbol placement depend on the locale, so avoid building currency output by concatenating a symbol and a number yourself.

Using the sprintf-js Library

If a format string with placeholders is easier to maintain, install the sprintf-js package with npm install sprintf-js and import its sprintf function. The %s placeholder accepts a string, while %.1f formats a number with one digit after the decimal point.

For example, with name set to "Bob" and score set to 95.5, the call sprintf("Player %s scored %.1f points", name, score) produces the message Player Bob scored 95.5 points. In a CommonJS project, obtain the function with const sprintf = require("sprintf-js").sprintf; in an ES module, import it with import { sprintf } from "sprintf-js". This package must be installed in the project before either import works.

Use this library when the format string itself is useful, such as when matching a printf-style convention. For a short message with a few values, a template literal is usually easier to read. Avoid adding a format method to String.prototype: changing a built-in prototype can conflict with other code, and a library or standalone helper keeps the behavior explicit.

Conclusion

Choose the tool that matches the value being formatted. Template literals combine text with variables and expressions; Intl.NumberFormat handles locale-aware numeric presentation; and sprintf-js supplies printf-style placeholders when that syntax is needed. JavaScript has no direct built-in printf or String.Format equivalent. Logging with console.log is useful for viewing a value, but it is separate from creating the string.

FAQ

  1. What is the JavaScript equivalent of printf?
    There is no built-in equivalent. Use a template literal for common interpolation, or sprintf-js when you need printf-style format specifiers.

  2. How do I format a number as currency?
    Call new Intl.NumberFormat(locale, { style: "currency", currency }).format(amount) and choose a locale and currency appropriate for the user.

  3. Can template literals span multiple lines?
    Yes. Newlines inside the backticks are part of the resulting string.

  4. Does console.log format strings like printf?
    No. console.log writes values to a console and is useful for debugging; it is not a general-purpose string formatter.

  5. Which method is fastest?
    There are no measurements here to support a performance ranking. Choose based on the required output and measure in your own application if formatting speed matters.