Teaching math object in JavaScript
8 minute(s) read

Last updated:
Jul 13, 2021 
These days, numbers and mathematics are being used in daily tasks such as shopping, sport, etc., in fact, these cases have formed a large part of our lives these days, you also know that computers, websites and various programs play an important role in people’s lives, so you can see the reason for the obsession with math and websites as well as various programs, ever since people were able to perform their mathematics calculations through computers as well as various websites, a great change has taken place in our lives, and we have seen how fast we can solve a complex problem.
One of the most popular programming languages is JavaScript, which if you have mastered it, you would be also able to do mathematical calculations with the help of numbers in it, in this article we will discuss it more but before that we need to give a brief explanation of the JavaScript programming language.
What is JavaScript?
It is one of the programming languages that can be used for clean coding and with the help of which complex features can be implemented in a web page.
JavaScript Math Object:
You need to learn this section in order to do different calculations, in this case, the Math object is static, now in order to understand it better, consider the following example.
<! DOCTYPE html>
<html>
<body>
<h2> JavaScript Math.PI </h2>
<p> Math.PI returns the ratio of a circle's circumference to its diameter: </p>
<p id = "demo"> </p>
<script>
document.getElementById ("demo"). innerHTML = Math.PI;
</script>
</body>
</html>
JavaScript Math Constants:
There are 8 math constants in this language that can be used, which are as follows.
<! DOCTYPE html>
<html>
<body>
<h2> JavaScript Math Constants </h2>
<p id = "demo"> </p>
<script>
document.getElementById ("demo"). innerHTML =
"<p> <b> Math.E: </b>" + Math.E + "</p>" +
"<p> <b> Math.PI: </b>" + Math.PI + "</p>" +
"<p> <b> Math.SQRT2: </b>" + Math.SQRT2 + "</p>" +
"<p> <b> Math.SQRT1_2: </b>" + Math.SQRT1_2 + "</p>" +
"<p> <b> Math.LN2: </b>" + Math.LN2 + "</p>" +
"<p> <b> Math.LN10: </b>" + Math.LN10 + "</p>" +
"<p> <b> Math.LOG2E: </b>" + Math.LOG2E + "</p>" +
"<p> <b> Math.Log10E: </b>" + Math.LOG10E + "</p>";
</script>
</body>
</html>
The result of this coding is as follows.
JavaScript Math Constants
Math.E: 2.718281828459045
Math.PI: 3.141592653589793
Math.SQRT2: 1.4142135623730951
Math.SQRT1_2: 0.7071067811865476
Math.LN2: 0.6931471805599453
Math.LN10: 2.302585092994046
Math.LOG2E: 1.4426950408889634
Math.Log10E: 0.4342944819032518
Math.method. (Number):
There are various methods in this field, some of which we are going to mention in the following with examples.
 Math.round ():
This method is used when you want to round a number to the nearest integer.
Math.round (4.9); // returns 5
Math.round (4.7); // returns 5
Math.round (4.4); // returns 4
Math.round (4.2); // returns 4
Math.round (4.2); // returns 4
In order to learn how to implement it, you can also pay attention to the following example.
<! DOCTYPE html>
<html>
<body>
<h2> JavaScript Math.round () </h2>
<p> Math.round (x) returns the value of x rounded to its nearest integer: </p>
<p id = "demo"> </p>
<script>
document.getElementById ("demo"). innerHTML = Math.round (4.8);
</script>
</body>
</html>
 Math.ceil ():
This method is used when you want the number to be rounded up to the nearest integer available, in a way that if your number is 4.3, with this method, the result will be 5, which means that it has been rounded up.
Note the following example, and it should be noted that the result of this example is 5.
<! DOCTYPE html>
<html>
<body>
<h2> JavaScript Math.ceil () </h2>
<p> Math.ceil () rounds a number <strong> up </strong> to its nearest integer: </p>
<p id = "demo"> </p>
<script>
document.getElementById ("demo"). innerHTML = Math.ceil (4.4);
</script>
</body>
</html>
 Math.floor ():
This method is also used to round numbers, which, as its name implies, will round numbers down.
Math.floor (6.9); // returns 6
Math.floor (7.7); // returns 7
Math.floor (5.6); // returns 5
Math.floor (6.2); // returns 6
Math.floor (8.6); // returns 8
The way to import this method is as follows.
<! DOCTYPE html>
<html>
<body>
<h2> JavaScript Math.floor () </h2>
<p> Math.floor (x) returns the value of x rounded <strong> down </strong> to its nearest integer: </p>
<p id = "demo"> </p>
<script>
document.getElementById ("demo"). innerHTML = Math.floor (4.7);
</script>
</body>
</html>
 Math.trunc ():
This is another method that is used to round numbers, and the result you will see is that it is rounded to a number without any fractional digits.
Math.trunc (5.6); // returns 5
Math.trunc (7.7); // returns 7
Math.trunc (8.6); // returns 8
Math.trunc (9.2); // returns 9
Math.trunc (8.2); // returns 8
 Math.sign ():
returns if x is negative, null or positive, now consider the following example in this regard.
<! DOCTYPE html>
<html>
<body>
<h2> JavaScript Math.sign () </h2>
<p> Math.sign (x) returns if x is negative, null or positive: </p>
<p id = "demo"> </p>
<script>
document.getElementById ("demo"). innerHTML = Math.sign (4);
</script>
</body>
</html>
 Math.pow ():
This method is used to calculate the power of numbers, in a way that the number written on the left side in parentheses to the power of the number on the right side in it.
Math.pow (2, 3); // returns 8
Math.pow (6, 2); // returns 36
Math.pow (5, 3); // returns 125
Math.pow (4, 3); // returns 64
 Math.sqrt ():
You need to use this method to perform calculations in which you want to get the square root of numbers.
Math.sqrt (16); // returns 4
Math.sqrt (25); // returns 5
Math.sqrt (36); // returns 6
Math.sqrt (81); // returns 9
 Math.abs ():
The result you will see from using this method is that you can see the absolute (positive) value of each number you enter this way.
Math.abs (7.5); // returns 7.5
Math.abs (6.8); // returns 6.8
Math.abs (9.3); // returns 9.3
Math.abs (5.4); // returns 5.4
 Math.sin ():
As the name implies, this method allows you to calculate the sine of different angles, and the value that can be seen in the result is between 1 and 1.
Another point about this method is that the unit of angle can be changed from degrees to radians.
Consider the following example.
<! DOCTYPE html>
<html>
<body>
<h2> JavaScript Math.sin () </h2>
<p> Math.sin (x) returns the sin of x (given in radians): </p>
<p> Angle in radians = (angle in degrees) * PI / 180. </p>
<p id = "demo"> </p>
<script>
document.getElementById ("demo"). innerHTML =
"The sine value of 90 degrees is" + Math.sin (90 * Math.PI / 180);
</script>
</body>
</html>
The result of this example will be as follows.
JavaScript Math.sin ()
JavaScript Math.sin()
Math.sin(x) returns the sin of x (given in radians):
Angle in radians = (angle in degrees) * PI / 180.
The sine value of 90 degrees is 1
 Math.min ():
This method is used to find the lowest value in the list of arguments.
Math.min (4, 687, 56, 40, 5, 300); // returns 300
 Math.max ():
This method is used to find the maximum value in the list of arguments.
Math.max (5, 440, 45, 28, 9, 300); // returns 440
 Math.random ():
This is used to find a random number between zero and one.
Consider the following example.
<! DOCTYPE html>
<html>
<body>
<h2> JavaScript Math.random () </h2>
<p> Math.random () returns a random number between 0 and 1: </p>
<p id = "demo"> </p>
<script>
document.getElementById ("demo"). innerHTML = Math.random ();
</script>
</body>
</html>
The result will be as follows.
JavaScript Math.random()
Math.random() returns a random number between 0 and 1:
0.17255598170336683
 Math.log ():
This method is used when returns the natural logarithm of x.
Math.log (1); // returns 0
 Math.log2 ():
returns the base 2 logarithm of x.
Consider the following example.
Math.log2 (8); // returns 3
 Math.log10 ():
returns the base 10 logarithm of x.
Consider the following example.
Math.log10 (1000); // returns 3
Last word:
In general, by using this language, mathematics becomes sweeter, and users can perform complex calculations in programming with the help of various methods that exist, in this article, a number of methods that can perform different calculations, such as finding the sine of different angles, finding the largest number among the various numbers in the list, rounding different numbers up, down, etc., have been mentioned.
Also, calculating the logarithm, the square root of the numbers, etc., are all tasks that can be done easily with the help of the existing methods which help you get accurate results from doing them, all of which are given in this regard with their examples, so that you can use them in your programming.
Click to audit your website SEO