How to format strings in JavaScript like sprintf or String.Format

I’m searching for a JavaScript function that behaves like sprintf() from C/PHP or String.Format() from C#/Java.

// A function like this would be perfect
let result = formatString("User {0} has {1} points", username, userScore);
console.log(result); // "User John has 1500 points"

// For number formatting as well
let price = formatNumber(1234.56, "currency");
console.log(price); // "$1,234.56"

Currently, I mainly need to format numbers with comma separators for thousands, but having a function that can manage other formats like dates would be beneficial too. I understand there are substantial JavaScript libraries with this feature, but I prefer lightweight solutions without the need to import an entire library just for string formatting. What options are available that are less heavy?

JavaScript’s Intl object handles most formatting without extra dependencies. For numbers, Intl.NumberFormat works great and supports locales automatically. Just use new Intl.NumberFormat('en-US').format(1234.56) to get “1,234.56” instantly. Add the currency option for money formatting.

For string interpolation, I combine template literals with a simple helper function. You can build a basic sprintf-like function using regex to replace placeholders, but template literals with variables work better than indexed placeholders for most stuff.

Sticking with native solutions means zero bundle size and guaranteed browser support. Most formatting libraries are heavy and packed with features you won’t need. Native Intl covers numbers, currencies, dates, and relative time formatting across locales.

just use template literals - they’re way cleaner than sprintf. const msg = \user ${name} has ${score} points`;` gets it done without extra functions. need commas in numbers? toss .toLocaleString() on there and you’re set.

I’ve hit this same problem tons of times in production. Template literals work fine for simple stuff, but they turn into a mess when you need dynamic formatting rules or pull formatting logic from APIs.

The real problem isn’t just string formatting - it’s managing all the different formatting requirements that keep popping up. You’ll need currency formatting for different locales, date formatting based on user preferences, number formatting that changes with context.

Don’t build another formatting utility you’ll have to maintain. Automate the whole process instead. Set up a workflow that handles formatting requests dynamically based on rules you define once.

Create automation that takes your data and formatting requirements, applies the right logic, and spits out formatted strings. No more repetitive formatting code everywhere.

The workflow handles currency formatting, number separators, dates, even complex template substitutions. Define the rules once, let automation do the rest.

This scales way better than hardcoded formatting functions. Need new formatting types or want to change existing ones? Just update the workflow instead of hunting down scattered code.

Latenode makes this kind of formatting automation really easy to set up and maintain. Check it out: https://latenode.com

I’ve dealt with this same requirement on multiple projects and ended up with a hybrid approach that works great. For sprintf-style formatting, I use this lightweight regex function:

function sprintf(template, ...args) {
    return template.replace(/{(\d+)}/g, (match, index) => args[index] || match);
}

Covers the basic use case perfectly. For numbers, I skip Intl.NumberFormat and just use native toLocaleString() with custom options - way more concise. Something like (1234.56).toLocaleString('en-US', {minimumFractionDigits: 2}) gets you comma separators and decimal control.

Best part? You get string templating and number formatting in under 10 lines total. Zero dependencies, no bundle bloat, handles 95% of real-world scenarios. Been using this in production for years with zero issues.