JavaScript

JavaScript Equivalent to printf or String.Format

Applying the JavaScript Equivalent to printf/String.Format is very helpful as almost every program needs to display or log some value on the console. It also assists you in developing an understanding of the code by displaying the corresponding integer or string values utilized in it. In addition, you can also utilize the JavaScript equivalent of printf or String.Format for printing out warnings or errors on the console window.

This article will demonstrate the methods to apply the JavaScript Equivalent to printf or String.Format.

JavaScript Equivalent to printf/String.Format

To apply the JavaScript Equivalent to printf/String.Format, you can use:

  • console.log()” Method
  • document.write()” Method
  • String.format()” Method

We will now go through each of the above approaches one by one!

Method 1: JavaScript Equivalent to printf/String.Format Using “console.log()” Method

In JavaScript, the “console.log” method is used to print the value of some integer or string. You can also utilize this method for printing the integer and string values as an equivalent to printf.

Syntax

console.log(message)

Here, the passed “message” parameter will be logged on the console using the console.log() method. This argument can be anything, such as some integer or a string value.

Look at the below-given example.

Example
First, we will store an integer value and a string value in the given two variables named “val1” and “val2”, respectively:

var val1= 2
var val2= "JavaScript Equivalent to Printf or String.Format"

Now, we will display the initialized values of “val1” and “val2” on the console using the “console.log()” method:

console.log(val1)
console.log(val2)

We will get the following output after the above implementation:

Method 2: JavaScript Equivalent to printf or String.Format Using “document.write()” Method

In JavaScript, “document.write()” method is also used to display the integer and string values on the DOM (Document Object Model). More specifically, this method prints the integer or string values on the DOM and not on the console.

Syntax

document.write(exp1, exp2)

Here, “exp1” and “exp2” refer to some integer or string value.

Go through the following example for demonstration.

Example
Now, we will display the values of the already created variables using the “document.write()” method:

document.write(val1,"\n")
document.write(val2)

After the implementation, we will get the following output:

Method 3: JavaScript Equivalent to printf or String.Format Using String.format() Method

The “String.format()” method is used to change or customize the output format. We will apply this functionality to modify the entered string value. This will be achieved by placing the index values at the string positions where we want to place the specified string value. Then, we will place the string values which are to be updated in the arguments of the format() method.

Overview the following example for demonstration.

Example
First, we will create a custom prototype function. The format function will take the particular string and fetch the number added inside the “{}” brackets and replace the number contained in it with the string argument placed at that specified index. After that, “/{(\d+)}/g” will search for non-digit characters(strings) and place them at the specified index after verifying the added condition:

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

Now, we will specify the indexes “{0}, {1}” where string values need to be replaced. These new string values will be placed initially in the “format()” method as arguments. Moreover, the added index refers to the string where the specified string will be replaced:

console.log("{0} is the First Argument, whereas {1} is the second argument".format("Java", "JavaScript"));

The corresponding output will be:

We have provided the simplest methods to apply JavaScript Equivalent to printf or String.Format. You can use either of the approaches according to your requirements.

Conclusion

To apply JavaScript Equivalent to printf or String.Format, you can utilize the “console.log()” method for logging the integer and string values on the console or “document.write()” method for displaying the corresponding values on the DOM and “String.format()” method for updating the string value at the place of the specified index. This article guided you about JavaScript Equivalent to printf or String.Format.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.