Teaching math object in JavaScript
6 minute(s) read
|
Published on: Jun 04, 2021
Updated on: Dec 14, 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 this 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-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.
Constants:
There are 8 this constants in this language that can be used, which are as follows.
The result of this coding is as follows.
JavaScript-Constants
.E: 2.718281828459045
.PI: 3.141592653589793
.SQRT2: 1.4142135623730951
.SQRT1_2: 0.7071067811865476
.LN2: 0.6931471805599453
.LN10: 2.302585092994046
.LOG2E: 1.4426950408889634
.Log10E: 0.4342944819032518
.method. (Number):
There are various methods in this field, some of which we are going to mention in the following with examples.
- .round ():
This method is used when you want to round a number to the nearest integer.
.round (4.9); returns 5
.roud (4.7); returns 5
.round (4.4) returns 4
.round (4.2); returns 4
.round (-4.2); returns -4
In order to learn how to implement it, you can also pay attention to the following example.
- 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.
- 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.
- .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.
.trunc (5.6); returns 5
.trunc (7.7); returns 7
ath.trunc (8.6); returns 8
.trunc (9.2); returns 9
.trunc (-8.2); returns -8
- .sign ():
returns if x is negative, null or positive, now consider the following example in this regard.
- 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.
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.
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.
.log (1); returns 0
- .log2 ():
returns the base 2 logarithm of x.
Consider the following example.
.log2 (8); returns 3
- .log10 ():
returns the base 10 logarithm of x.
Consider the following example.
.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