JavaScript Equivalent to printf or String.format Syntax Conversion

If you wanted to know the equivalent of JavaScript printf or String.formatkeep on reading!

The printf and String.format is popular string formatting tools in other programming languages such as C, PHP, and C#.

However, JavaScript does not have a built-in equivalent to these functions. 

Instead, developers can use several approaches to achieve similar functionality in JavaScript.

But before we proceed to the printf equivalent in js or JavaScript, let’s first understand what is printf.

What is “printf”?

The printf function is used to create a string based on a format string and a list of values in the C, PHP, and C# programming language.

The format string acts as a template, where characters are usually copied as they are, but special codes starting with % are used to represent data and determine how it should be displayed in the string.

So, the printf function takes these inputs and generates the desired output string by replacing the format specifiers with the corresponding values.

What are the equivalent methods of the “printf()” function or “String.format()” in JavaScript?

There are various methods that we can use as an equivalent method of the printf() function or String.format() in js or JavaScript. Let’s start to explore the following solutions:

Use “console.log()” method

In JavaScript, you can use the console.log() method to display the value of an integer or string. It can be used as an alternative to printf for printing integer and string values.

In addition to that, the console.log() method serves as the counterpart to the printf() function in C.

For example:

console.log("Hi, Welcome to Itsourcecode!");

Output:

Hi, Welcome to Itsourcecode!

Here’s another example:

var a = 100;
var b = 509;
console.log(`The sum of ${a} and ${b} is ${a + b}.`);

Output:

The sum of 100 and 509 is 609.

In JavaScript, besides ‘console.log()’, you can also utilize ‘console.info()’, ‘console.warn()’, and ‘console.error()’ to show messages with different levels of importance in the console.

Use “document.write()” method

In JavaScript, the document.write() method is used to show integer and string values on a web page. It specifically prints these values on the web page itself, not in the console.

For example:

var sample1= 100
var sample2= "This is just a sample"
console.log(sample1)
console.log(sample2)

We are going to use this example as our basis.

document.write(sample1, sample2)

Next, we will use the document.write()method to show the values of the variables that have already been created.

document.write(sample1,"\n")
document.write(sample2)

Output:

100
This is just a sample

Use String.format() method

If you want to personalize a string, you can use the String.format() function. It allows you to easily modify the string to your liking.

Basically, this function looks for curly brackets {} within the string and replaces them with the corresponding values provided after the .format keyword.

For example:

String.prototype.format = function () {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function (match, number) {
        return typeof args[number] != "undefined" ? args[number] : match;
        });
    };

Now, we will specify the positions “{0}, {1}” where we want to replace string values. These new string values will be provided as arguments to the “format()” method.

The index indicates the string where the specified value will be inserted.

    console.log("{0} is my country, and  {1} is the capital of  {0}, {2}".format("USA", "Washington DC"));

Output:

USA is my country, and  Washington DC is the capital of  USA, {2}

Conclusion

In conclusion, this article explains that JavaScript lacks built-in functions like printf or String.format found in other languages. We already suggested alternative methods for achieving similar functionality.

These methods include using console.log() to display values, document.write() to show values on a web page, and String.format() for personalized string formatting.

We are hoping that this article provides you insights into ways to format strings in JavaScript.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment