Help us fix this page
If you found a broken link, missing page, or incorrect redirect, please let us know. Your report helps us improve the website for everyone.

String formatting is a fundamental aspect of programming, allowing developers to create dynamic and readable text output by embedding variables and applying specific formatting rules. The sprintf function, originating from the C programming language, has become a standard for this purpose across numerous languages.
JavaScript doesn’t have a built-in sprintf function. Libraries like Playful Sparkle’s sprintf.js bridge this gap, bringing the familiar and robust sprintf functionality to the JavaScript ecosystem.
sprintf() provides precise control over string formatting-padding, alignment, decimal precision, and type conversion-that template literals and concatenation cannot match.sprintf.js is a lightweight, open-source library that faithfully implements the standard sprintf behavior in JavaScript.sprintf() for variadic arguments, vsprintf() for array arguments.%[argument_index$][flags][width][.precision]type-supporting 15+ type specifiers including strings, integers, floats, booleans, hex, and octal."User %(name)s is %(age)d years old.""%2$s %1$s" outputs “Hello World”.JavaScript has three main ways to build dynamic strings:
1. Concatenation (+)
const name = "Alice";
const age = 30;
const message = "My name is " + name + " and I am " + age + " years old.";2. Template literals (backticks)
const message = `My name is ${name} and I am ${age} years old.`;3. sprintf
const message = sprintf("My name is %s and I am %d years old.", name, age);For simple cases, template literals work well. But when you need:
sprintf provides a structured, declarative approach that template literals cannot match.
Note:sprintf.js is not a replacement for template literals-it’s a specialized tool for cases where formatting requirements exceed what native syntax provides.
The sprintf paradigm exists across many programming languages:
| Language | Function | Example |
|---|---|---|
| C | printf() / sprintf() | printf("%s", "Hello") |
| PHP | sprintf() | sprintf("%s", "Hello") |
| Python | % operator / .format() / f-string | "%s" % "Hello" |
| Java | String.format() | String.format("%s", "Hello") |
| Ruby | % / sprintf | "%s" % "Hello" |
| Go | fmt.Sprintf() | fmt.Sprintf("%s", "Hello") |
JavaScript is the outlier. Libraries like sprintf.js fill this gap.
Playful Sparkle’s sprintf.js is a well-maintained, lightweight, open-source library that implements standard sprintf functionality in JavaScript.
sprintf behaviorNPM:
npm install @playfulsparkle/sprintf-jsYarn:
yarn add @playfulsparkle/sprintf-jsCDN (browser):
<script src="https://cdn.jsdelivr.net/npm/@playfulsparkle/sprintf-js/dist/sprintf.min.js"></script>sprintf(format, arg1, arg2, ...)Takes a format string and variadic arguments. Returns the formatted string.
import { sprintf } from '@playfulsparkle/sprintf-js';
const name = "Charlie";
const score = 95;
const message = sprintf("Student %s achieved a score of %d.", name, score);
// Output: "Student Charlie achieved a score of 95."
const pi = Math.PI;
const formattedPi = sprintf("Pi is approximately %.2f.", pi);
// Output: "Pi is approximately 3.14."vsprintf(format, args)Same as sprintf(), but accepts an array of arguments instead of variadic arguments. Useful when you have a dynamic array of values.
import { vsprintf } from '@playfulsparkle/sprintf-js';
const data = ["David", 88, "Science"];
const message = vsprintf("Student %s scored %d in %s.", data);
// Output: "Student David scored 88 in Science."The power of sprintf.js lies in its placeholders. The general syntax is:
%[argument_index$][flags][width][.precision]type| Specifier | Description | Example | Output |
|---|---|---|---|
% | Literal percent sign | sprintf("%%") | % |
b | Binary (integer) | sprintf("%b", 10) | 1010 |
c | Character (ASCII) | sprintf("%c", 65) | A |
d / i | Signed decimal integer | sprintf("%d", 123) | 123 |
e | Scientific notation (lowercase) | sprintf("%e", 123.45) | 1.234500e+2 |
E | Scientific notation (uppercase) | sprintf("%E", 123.45) | 1.234500E+2 |
f | Floating point decimal | sprintf("%f", 12.345) | 12.345000 |
g | Shorter of %e and %f | sprintf("%g", 123.45) | 123.45 |
G | Shorter of %E and %F | sprintf("%G", 123.45) | 123.45 |
o | Octal (integer) | sprintf("%o", 10) | 12 |
s | String | sprintf("%s", "Hello") | Hello |
t | Boolean (lowercase) | sprintf("%t", true) | true |
T | Boolean (uppercase) | sprintf("%T", false) | FALSE |
u | Unsigned decimal integer | sprintf("%u", -10) | 4294967286 |
x | Hexadecimal (lowercase) | sprintf("%x", 255) | ff |
X | Hexadecimal (uppercase) | sprintf("%X", 255) | FF |
Flags modify how the output is formatted:
| Flag | Effect |
|---|---|
+ | Always include a sign (+ or -) for numeric types |
| (space) | Insert a space before positive numbers |
0 | Pad with leading zeros instead of spaces |
- | Left-justify within the field width |
Specifies the minimum number of characters to output. If the output is shorter, it’s padded.
sprintf("%10s", "Test") // Output: " Test" (right-aligned, width 10)
sprintf("%-10s", "Test") // Output: "Test " (left-aligned, width 10)For floats: number of digits after the decimal point. For strings: maximum length.
sprintf("%.2f", 3.14159) // Output: "3.14"
sprintf("%.5s", "JavaScript") // Output: "JavaS"Explicitly specify which argument to use with n$ where n is the 1-based argument index.
sprintf("%2$s %1$s", "World", "Hello") // Output: "Hello World"Use %(name)type syntax with an object argument for clearer code.
import { sprintf } from '@playfulsparkle/sprintf-js';
const user = { name: "Jane", age: 25 };
const message = sprintf("User %(name)s is %(age)d years old.", user);
// Output: "User Jane is 25 years old."import { sprintf } from '@playfulsparkle/sprintf-js';
const timestamp = new Date().toISOString();
const level = "ERROR";
const message = "Failed to connect to database.";
const log = sprintf("[%s] [%s] %s", timestamp, level, message);
// Output: "[2026-08-02T12:30:45.000Z] [ERROR] Failed to connect to database."import { sprintf } from '@playfulsparkle/sprintf-js';
const userId = 123;
const baseUrl = "https://api.example.com/users/%d";
const apiUrl = sprintf(baseUrl, userId);
// Output: "https://api.example.com/users/123"import { sprintf } from '@playfulsparkle/sprintf-js';
const rows = [
["Alice", 95],
["Bob", 87],
["Charlie", 102]
];
for (const [name, score] of rows) {
console.log(sprintf("%-10s %3d", name, score));
}
// Output:
// Alice 95
// Bob 87
// Charlie 102import { sprintf } from '@playfulsparkle/sprintf-js';
const price = 49.99;
const discount = 0.15;
const finalPrice = price * (1 - discount);
const message = sprintf("Original: $%.2f | Discount: %.0f%% | Final: $%.2f",
price, discount * 100, finalPrice);
// Output: "Original: $49.99 | Discount: 15% | Final: $42.49"import { sprintf } from '@playfulsparkle/sprintf-js';
const color = 0xFF00FF;
console.log(sprintf("Hex: %X", color)); // "Hex: FF00FF"
console.log(sprintf("Binary: %b", 42)); // "Binary: 101010"import { sprintf } from '@playfulsparkle/sprintf-js';
const firstName = "John";
const lastName = "Doe";
console.log(sprintf("Last name: %2$s, First name: %1$s", firstName, lastName));
// Output: "Last name: Doe, First name: John"import { sprintf } from '@playfulsparkle/sprintf-js';
// Example: template loaded from a translation file or database
const emailTemplate = "Dear %s,\n\nThank you for your order #%d.\n\nRegards,\n%s";
const customerName = "Jane Smith";
const orderId = 12345;
const teamName = "Support Team";
const email = sprintf(emailTemplate, customerName, orderId, teamName);| Use Case | Template Literals | sprintf.js |
|---|---|---|
| Simple variable interpolation | Preferred | Works but overkill |
| Padding with zeros or spaces | Not supported | Native |
| Decimal precision control | Manual .toFixed() | Native |
| Type conversion (hex, octal, binary) | Manual toString() | Native |
| Argument reordering | Not supported | Native |
| Loading templates from external sources | Limited | Native |
| Internationalization (format strings) | Limited | Works well |
| Integration with localization frameworks | Limited | Common approach |
Recommendation: Use template literals for basic interpolation. Reach for sprintf.js when your formatting requirements exceed what template literals can handle natively.
sprintf.js works in:
Node.js:
Browsers:
Note: For older browsers (IE11 and earlier), you may need polyfills for certain JavaScript features.
Ensure you’ve installed the package:
npm install @playfulsparkle/sprintf-jsES Modules (import):
import { sprintf } from '@playfulsparkle/sprintf-js';CommonJS (require):
const { sprintf } = require('@playfulsparkle/sprintf-js');If the output doesn’t match expectations, verify the type specifier matches the argument type:
javascript
sprintf("%s", 123) // "123" (converted to string)
sprintf("%d", "123") // "123" (converted to integer)
sprintf("%s", null) // "null"sprintf. Libraries like sprintf.js fill this gap with a familiar, powerful API.sprintf() offers formatting controls-padding, precision, type conversion, and argument reordering-that template literals cannot match.sprintf(format, ...args) for variadic arguments and vsprintf(format, args[]) for array arguments.%[argument_index$][flags][width][.precision]type-learn the type specifiers to use the library effectively."User %(name)s is %(age)d years old."sprintf.js is for complex formatting requirements.String formatting is a fundamental skill for JavaScript developers. While template literals have improved the language’s string handling capabilities, sprintf.js provides a level of control and precision that native syntax cannot match.
Whether you’re building log formatters, generating dynamic URLs, preparing localized content, or formatting tabular data, sprintf.js offers a reliable, well-tested solution with a familiar API for developers coming from other languages.
Explore sprintf.js further on its GitHub repository: https://github.com/playfulsparkle/sprintf.js
Need help with your JavaScript development? Playful Sparkle has been engineering digital products since 2004, offering Web Development, UI/UX & Web Design, and App Development services. Contact us to discuss how we can help with your next project.