JavaScript analog of printf and String.Format: full breakdown

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

JavaScript analog of printf and String.Format: how to format strings like a pro?



Have you ever written code like this in JavaScript?



let name = 'Alexey';let age = 28;console.log('Hello, my name is ' + name + ' and I am ' + age + ' years old.');


If yes — you definitely know how annoying this is. Constantly opening and closing quotes, adding plus signs, getting confused with spaces. And what if you need to insert 5-6 variables? A nightmare!



In other languages, this problem was solved long ago. Python has f"Hello, {name}" and "%s %d" % (name, age). C has the legendary printf. C# and Java have String.Format. But what about JavaScript?



Spoiler: there is a solution, and more than one. In this article, we will cover all ways to format strings in JavaScript — from classic to modern. You will learn which JavaScript analog of printf to choose for your task, and forget about concatenation forever.



Why is the question "JavaScript equivalent to printf/String.Format" so popular?



On Stack Overflow, this question has gathered hundreds of thousands of views. And this is no coincidence. Every developer switching from another language to JavaScript first looks for a familiar string formatting tool.



The problem is that pure JavaScript has no built-in analog of printf or String.Format. There is only concatenation (joining strings with +). But developers have found several elegant solutions, and we will cover them.



Method 1: Template Literals — the modern standard



Starting with ES6 (2015), JavaScript introduced template strings. This is the best analog of printf and String.Format for everyday use. They work in all modern browsers and Node.js.



Syntax: use backticks ` instead of regular quotes, and insert variables with ${}.



let name = 'Maria';let age = 25;let city = 'Moscow';

// Instead of concatenationlet message = `Hello, my name is ${name}, I am ${age} years old, I am from ${city}.`;console.log(message); // Output: Hello, my name is Maria, I am 25 years old, I am from Moscow.


But that's not all! Inside ${} you can execute any JavaScript expressions:



let a = 10;let b = 5;

console.log(`Sum: ${a + b}`); // Sum: 15console.log(`Greater? ${a > b ? 'yes' : 'no'}`); // Greater? yesconsole.log(`Random: ${Math.random()}`); // Random: 0.4837...


Why is this the best analog of printf in JavaScript?

  • Readability — code looks like regular text with insertions
  • Safety — escaping happens automatically
  • Multiline — you can write text over multiple lines
  • Expressions — you can insert computation results


Multiline template strings



let html = `<div class="card">  <h2>${title}</h2>  <p>${description}</p></div>`;

console.log(html);// Output — neat HTML with preserved line breaks


Compare this with concatenation — the difference is enormous!



Method 2: The sprintf-js library — classic printf in JavaScript



If you miss printf from C or %-formatting from Python, you have an excellent option — the sprintf-js library.



Installation via npm:

npm install sprintf-js


Usage:



const sprintf = require('sprintf-js').sprintf;const vsprintf = require('sprintf-js').vsprintf;

// Simple formattinglet result = sprintf('Hello, %s! You are %d years old.', 'Ivan', 30);console.log(result); // Hello, Ivan! You are

Blogs

Book Recommendations