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
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 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(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
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(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:
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:
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.