JavaScript formatter
Posted by Superadmin on May 02 2023 14:17:26

JavaScript formatter

JavaScript formatter

Introduction to JavaScript formatter

In JavaScript, a formatter is used to reduce the number of whitespaces involved while writing the code, reduce the size of the data that needs to be transferred, and provide an overall enhanced code format to the end-user. It beautifies the entire set of codes by providing proper indentation and modification. JavaScript formatter comprises formatting in the level of text, String, object, and code depending on the user which level of formatting will be chosen to enhance the code for reusability and understandability. JavaScript Formatter provides a standard to the code.

Syntax

Here is the syntax for formatting a string using Formatter in JavaScript :

String.prototype.format = function () {
var ai = this;
for (var z in arguments) {
ai = ai.replace("{" + z + "}", arguments[z])
}
return ai
}

First, the function is assigned to the string format, and then it is replaced with the variable for the working, and then the formatting of the string is performed accordingly. Finally, the values of the string assigned in the reserved variable are represented in the syntax flow.

How JavaScript formatter works?

Examples of JavaScript formatter

Below are the examples of JavaScript formatted:

Example #1

This program demonstrates the formatter where the user can format the HTML-based text by writing the text in a paragraph format and providing different features and format using tags like here the paragraph can be right-aligned, followed by the changed font and color of the text font as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<p> This paragraph needs can be edited and changed. </p>
<p align= "Right"> This paragraph can be edited and aligned towards Right.</p>
<p><b>The signature and font of the article can also be changed.</b></p>
<p><font color="Violet">The Font color can be formatted into Green color.</font></p>
</body>
</html>

Output:

JavaScript formatter output 1

Example #2

This program demonstrates the string format of a text in the JavaScript to modify the string format in italics once the submit button is clicked, as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press on the button to display the string format in Italics</p>
<button onclick="Func()">Submit</button>
<p id="dm_1"></p>
<script>
function Func() {
var s_r = "Hi_Everyone!";
var otpt = s_r.italics();
document.getElementById("dm_1").innerHTML = otpt;
}
</script>
</body>
</html>

Output:

JavaScript formatter output 2.1

JavaScript formatter output 2.2

Example #3

This program demonstrates the variable and then creates a new date using JavaScript short Dates as a formatter representing the date in the “DD/MM/YYYY” syntax as shown in the output and helps transform the values accordingly.

Code:

<!DOCTYPE html>
<html>
<body>
<h2>Creating_JS_dt</h2>
<p id="dm1"></p>
<script>
var d_t = new Date("09/22/2020");
document.getElementById("dm1").innerHTML = d_t;
</script>
</body>
</html>

Output:

output 3

Example #4

This program demonstrates the string formatter length and the property related to it for that string object, measuring the length and displaying it as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<h2>JS_String_formatter_length</h2>
<p>Length_Of_the_String.</p>
<p id="dm2"></p>
<script>
var t_x = "World_is_full_of_Misery.";
var s_ln = t_x.length;
document.getElementById("dm2").innerHTML = s_ln;
</script>
</body>
</html>

Output:

output 4

Example #5

This program demonstrates the JavaScript Formatting using ECMA Script in the object of the defined property and then manipulating all the values accordingly, which especially tells to change the values of the property object as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<h2>JS Define_Object_Property</h2>
<p>Change in the Object's Property value.</p>
<p id="dm_3"></p>
<script>
var emp = {
emp_Name: "Anu",
emp_id : "18",
emp_dept : "CSE"
};
Object.defineProperty(emp, "emp_dept", {value:"CSE"})
document.getElementById("dm_3").innerHTML = emp.emp_dept;
</script>
</body>
</html>

Output:

output 5

Conclusion

JavaScript Formatter plays a pivotal role in web development as it helps developers write the code by giving them the ability to enhance, beautify, and modify the code. Furthermore, since many developers work in coordination, they get the privilege to maintain consistency in the code during implementation with a better understanding, usability, and easiness.