The function sprintf is common in many programming languages, as a way to merge programme variables in a text string. It has good options for formatting, such as outlining text string, or setting the precision of numbers. In many cases, you don’t need those, and a simple function that just inserts the variables at the right place is fine, and a better option in order to keep your JavaScript code short. See the example below:
// Simple string concatenation var html1 = '<a href="' + url + '">' + link + '</a>'; // Using sprintf var html2 = sprintf( '<a href="%s">%s</a>', url, link );
In this article, I share a simple function doing string replacements. For sprintf alternatives with the full functionality, just Google on ‘javascript sprintf’, and you will find some good versions. If you don’t need all that, read on.