Header Ads Widget

JavaScript Math

 This post will describe about JavaScript Math and JavaScript Math methods.

What is JavaScript Math?

Math is a built-in object in JavaScript. Math object is used to perform mathematical calculation on numbers. Like other object Math object have methods and properties to perform mathematical operations. It is static because you can call without creating it.

JavaScript math
JavaScript math


JavaScript Math methods

Syntax of Math methods

Math.method()
Or
Math.method(number);

Following methods are used with Math object.

1. Math.abs()

Returns absolute (positive) value of negative number.
Example:

<script>
document.write(Math.abs(-6)); //6
document.write(Math.abs(-41.2)); //41.2
</script>

2. Math.acos()

Returns arccosine of an 0, 1 and -1 values. Returns NaN for string.
Example:

<script>
document.write(Math.acos(0)); //1.5707963267948966
document.write(Math.acos(a)); // NaN
document.write(Math.acos(1)); // 0
document.write(Math.acos(-1)); 
// 3.141592653589793
</script>

3. Math.acosh()

Returns hyperbolic arccosine of numeric value. Returns NaN for 0 and negative numbers.
Example:

<script>
document.write(Math.acosh(2)); 
// 1.3169578969248166
document.write(Math.acosh(0)); // NaN
document.write(Math.acosh(-6)); // NaN
</script>

4. Math.asin()

Returns arcsine of numeric value. It returns value between -PI/2 and PI/2. Returns NaN for other numbers.
Example:

<script>
document.write(Math.asin(0)); // 0
document.write(Math.asin(-1)); 
// -1.5707963267948966
document.write(Math.asin(1)); 
// 1.5707963267948966
</script>

5. Math.asinh()

Returns hyperbolic arcsine of numeric value.
Example:

<script>
document.write(Math.asinh(0)); //0
document.write(Math.asinh(1)); //0.881373587019543
</script>

6. Math.atan()

Returns arctangent of number between -PI/2 and PI/2.
Example:

<script>
document.write(Math.atan(0)); // 0
document.write(Math.atan(1)); // 0.7853981633974483
document.write(Math.atan(7)); //1.4288992721907328
</script>

7. Math.atan2()

Returns arctangent of quotient of two it’s arguments between -PI and PI radiance.
Example:

<script>
document.write(Math.atan2(9,8));
// 0.844153986113171
</script>

8. Math.cbrt()

Returns cubic root of specified argument.
Example:

<script>
document.write(Math.cbrt(8)); //2
</script>

9. Math.ceil()

Returns rounded value of specified floating point number which is greater than specified value in argument.
Example:

<script>
document.write(Math.ceil(5.4)); // 6
</script>

10. Math.cos()

Returns cosine of specified argument.
Example:

<script>
document.write(Math.cos(6));
//  0.9601702866503661
</script>

11. Math.cosh()

Returns hyperbolic cosine of number.
Example:

<script>
document.write(Math.cosh(5)); 
// 74.20994852478785
</script>

12. Math.exp()

Returns the value of En . (Where E is Euler’s number and is a number passed to it)
Example:

<script>
document.write(Math.exp(1)); 
// 2.718281828459045
</script>

13. Math.floor()

Returns rounded value of specified floating point number which is smaller than specified value in argument.
Example:

<script>
document.write(Math.floor(4.5)); //4
</script>

14. Math.log()

Returns logarithm of number. 
Example:

<script>
document.write(Math.log(5)); //1.6094379124341003
</script>

15. Math.min()

Returns smallest number from given argument. In argument list more than one numbers should be passed.
Example:

<script>
document.write(Math.min(5,2,3)); //2
</script>

16. Math.max()

Returns biggest number among the numbers passed as argument. More than one numbers should be specified in argument list.
Example:

<script>
document.write(Math.max(4,9,21)); //21
</script>

17. Math.pow()

Returns the value of first number to the power of second number. First number is called base and second number is exponent of base. Both are required.
Example:

<script>
document.write(Math.pow(4,3)); //64
</script>

18. Math.random()

Returns random number between 0 to 1 integers. Every time you run the code it will returns different number.
Example:

<script>
document.write(Math.random());
//0.09530382054591868
</script>

Example: Display random number between 0 and 10.

<script>
document.write(Math.random()*10); 
// 4.06593319046199
</script>

Example: Display random number between 0 and 10 with rounded value.

<script>
document.write(Math.floor(Math.random()*10)); //4
</script>

19. Math.round()

Returns rounded value of specified floating point value to the nearest integer. 
Example:

<script>
document.write(Math.round(5.1)); //5
document.write(Math.round(5.76)); //6
</script>

20. Math.sign()

Returns whether specified number is positive, negative or zero. Returns NaN for string. For positive value 1, for negative value-1 and for zero 0 is returned .
Example:

<script>
document.write(Math.sign(0)); // 0
document.write(Math.sign(3)); // 1
document.write(Math.sign(-3)); // -1
</script>

21. Math.sin()

Returns sine value of specified integer.
Example:

Example:

<script>
document.write(Math.sin(2)); //0.9092974268256817
</script>

22. Math.sinh()

Returns hyperbolic sine value of the specified number.
Example:
<script>
document.write(Math.sinh(2)); //3.626860407847019
</script>

23. Math.sqrt()

Returns square root of specified number.
Example:

<script>
document.write(Math.sqrt(144)); //12
</script>

24. Math.tan()

Returns tangent of specified number.
Example:

<script>
document.write(Math.tan(1)); //1.5574077246549023
</script>

25. Math.tanh()

Returns hyperbolic tangent of specified number.
Example:

<script>
document.write(Math.tanh(2)); //0.9640275800758169
</script>

26. Math.trunc()

Returns a number without decimal point. It removes numbers after decimal point and returns numbers before decimal point without rounding number.
Example:

<script>
document.write(Math.trunc(34.574)); //34
</script>

Summary 

This post covers Math object methods. Unlike other objects that Math object not create constructor. This methods are shortest way to perform mathematical calculation.

Post a Comment

0 Comments