DEV Community

Vigneshwaran V
Vigneshwaran V

Posted on

Math Functions in Js

In JavaScript, mathematical functions are provided as static methods on the built-in Math object. Because it is a static namespace rather than a constructor, you call these functions directly as Math.methodName() without instantiating it.

  • We use Math only on Number data type and not on BigInt

The Math Object

  • The JavaScript Math object allows you to perform mathematical tasks.

  • The Math object is static.

  • All methods and properties can be used without creating a Math object first.

Math Properties (Constants)

The syntax for any Math property is : Math.property.
JavaScript provides 8 mathematical constants that can be accessed as Math properties:

This example uses math object properties to return their values.

Math.E        // returns Euler's number
Math.PI       // returns PI
Math.SQRT2    // returns the square root of 2
Math.SQRT1_2  // returns the square root of 1/2
Math.LN2      // returns the natural logarithm of 2
Math.LN10     // returns the natural logarithm of 10
Math.LOG2E    // returns base 2 logarithm of E
Math.LOG10E   // returns base 10 logarithm of E
Enter fullscreen mode Exit fullscreen mode

Output

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
Enter fullscreen mode Exit fullscreen mode

Math Constants

Constants are fixed values provided by the Math object.
1. Math.PI
Returns the value of π (Pi).
Definition: The ratio of a circle's circumference to its diameter.

console.log(Math.PI);
Enter fullscreen mode Exit fullscreen mode

Output

3.141592653589793
Enter fullscreen mode Exit fullscreen mode

Example

let radius = 5;
let area = Math.PI * radius * radius;

console.log(area);
Enter fullscreen mode Exit fullscreen mode

Output

78.53981633974483
Enter fullscreen mode Exit fullscreen mode

2. Math.E
Returns Euler's number.
Definition: The base of natural logarithms.

console.log(Math.E);
Enter fullscreen mode Exit fullscreen mode

Output

2.718281828459045
Enter fullscreen mode Exit fullscreen mode

3. Math.SQRT2
Returns the square root of 2.

console.log(Math.SQRT2);
Enter fullscreen mode Exit fullscreen mode

Output

1.4142135623730951
Enter fullscreen mode Exit fullscreen mode

4. Math.SQRT1_2
Returns the square root of 1/2.

console.log(Math.SQRT1_2);
Enter fullscreen mode Exit fullscreen mode

Output

0.7071067811865476
Enter fullscreen mode Exit fullscreen mode

5. Math.LN2
Returns the natural logarithm of 2.

console.log(Math.LN2);
Enter fullscreen mode Exit fullscreen mode

Output

0.6931471805599453
Enter fullscreen mode Exit fullscreen mode

Example
To verify that ln(2) equals Math.LN2:

console.log(Math.log(2));
console.log(Math.LN2);
Enter fullscreen mode Exit fullscreen mode

Output

0.6931471805599453
0.6931471805599453
Enter fullscreen mode Exit fullscreen mode

6. Math.LN10
Returns the natural logarithm of 10.

console.log(Math.LN10);
Enter fullscreen mode Exit fullscreen mode

Output

2.302585092994046
Enter fullscreen mode Exit fullscreen mode

Example

console.log(Math.log(10));
console.log(Math.LN10);
Enter fullscreen mode Exit fullscreen mode

Output

2.302585092994046
2.302585092994046
Enter fullscreen mode Exit fullscreen mode

7. Math.LOG2E
Returns the base-2 logarithm of E.

console.log(Math.LOG2E);
Enter fullscreen mode Exit fullscreen mode

Output

1.4426950408889634
Enter fullscreen mode Exit fullscreen mode

8. Math.LOG10E
Returns the base-10 logarithm of E.

console.log(Math.LOG10E);
Enter fullscreen mode Exit fullscreen mode

Output

0.4342944819032518
Enter fullscreen mode Exit fullscreen mode

Math Methods

The syntax for Math any methods is : Math.method(number)

1. Math.abs()

Returns the absolute (positive) value of a number.

console.log(Math.abs(-25));
Enter fullscreen mode Exit fullscreen mode

Output

25
Enter fullscreen mode Exit fullscreen mode

2. Math.round()

Rounds a number to the nearest integer.

console.log(Math.round(4.6)); // 5
console.log(Math.round(4.4)); // 4
Enter fullscreen mode Exit fullscreen mode

3. Math.ceil()

Rounds a number upward to the nearest integer.

console.log(Math.ceil(4.1));
Enter fullscreen mode Exit fullscreen mode

Output

5
Enter fullscreen mode Exit fullscreen mode

4. Math.floor()

Rounds a number downward to the nearest integer.

console.log(Math.floor(4.9));
Enter fullscreen mode Exit fullscreen mode

Output

4
Enter fullscreen mode Exit fullscreen mode

5. Math.trunc()

Removes the decimal part and returns only the integer portion.

console.log(Math.trunc(4.99));
Enter fullscreen mode Exit fullscreen mode

Output

4
Enter fullscreen mode Exit fullscreen mode

6. Math.random()

Returns a random number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random());
Enter fullscreen mode Exit fullscreen mode

Output

0.735621
Enter fullscreen mode Exit fullscreen mode

Generate Random Number Between 0 and 9

let num = Math.floor(Math.random() * 10);
console.log(num);
Enter fullscreen mode Exit fullscreen mode

Generate Random number from 1 to 10

let num = Math.floor(Math.random() * 10) + 1;
console.log(num);
Enter fullscreen mode Exit fullscreen mode

7. Math.max()

Returns the largest value from a list of numbers.

console.log(Math.max(10, 20, 30, 40));
Enter fullscreen mode Exit fullscreen mode

Output

40
Enter fullscreen mode Exit fullscreen mode

8. Math.min()

Returns the smallest value from a list of numbers.

console.log(Math.min(10, 20, 30, 40));
Enter fullscreen mode Exit fullscreen mode

Output

10
Enter fullscreen mode Exit fullscreen mode

9. Math.pow()

Raises a number to a specified power.

console.log(Math.pow(2, 3));
Enter fullscreen mode Exit fullscreen mode

Output

8
Enter fullscreen mode Exit fullscreen mode

Modern Alternative

console.log(2 ** 3);
Enter fullscreen mode Exit fullscreen mode

10. Math.sqrt()

Returns the square root of a number.

console.log(Math.sqrt(64));
Enter fullscreen mode Exit fullscreen mode

Output

8
Enter fullscreen mode Exit fullscreen mode

11. Math.cbrt()

Returns the cube root of a number.

console.log(Math.cbrt(27));
Enter fullscreen mode Exit fullscreen mode

Output

3
Enter fullscreen mode Exit fullscreen mode

12. Math.sign()

Returns whether a number is positive, negative, or zero.

console.log(Math.sign(20));  // 1
console.log(Math.sign(-20)); // -1
console.log(Math.sign(0));   // 0
Enter fullscreen mode Exit fullscreen mode

13. Math.exp()

Returns e raised to the given power.

console.log(Math.exp(2));
Enter fullscreen mode Exit fullscreen mode

Output

7.38905609893065
Enter fullscreen mode Exit fullscreen mode

Example

console.log(Math.exp(1)); // same as Math.E
Enter fullscreen mode Exit fullscreen mode

14. Math.log()

Returns the natural logarithm (base e).

console.log(Math.log(Math.E));
Enter fullscreen mode Exit fullscreen mode

Output

1
Enter fullscreen mode Exit fullscreen mode

15. Math.log2()

Returns the base-2 logarithm.

console.log(Math.log2(8));
Enter fullscreen mode Exit fullscreen mode

Output

3
Enter fullscreen mode Exit fullscreen mode

Because

2 ** 3 = 8
Enter fullscreen mode Exit fullscreen mode

16. Math.log10()

Returns the base-10 logarithm.

console.log(Math.log10(1000));
Enter fullscreen mode Exit fullscreen mode

Output

3
Enter fullscreen mode Exit fullscreen mode

Because

10 ** 3 = 1000
Enter fullscreen mode Exit fullscreen mode

17. Math.sin()

Returns the sine of an angle (in radians).

console.log(Math.sin(Math.PI / 2));
Enter fullscreen mode Exit fullscreen mode

Output

1
Enter fullscreen mode Exit fullscreen mode

18. Math.cos()

Returns the cosine of an angle (in radians).

console.log(Math.cos(0));
Enter fullscreen mode Exit fullscreen mode

Output

1
Enter fullscreen mode Exit fullscreen mode

19. Math.tan()

Returns the tangent of an angle (in radians).

console.log(Math.tan(Math.PI / 4));
Enter fullscreen mode Exit fullscreen mode

Output

1
Enter fullscreen mode Exit fullscreen mode

20. Math.hypot()

  • Returns the square root of the sum of squares of its arguments.
  • Returns the hypotenuse length using the Pythagorean theorem.
console.log(Math.hypot(3, 4));
Enter fullscreen mode Exit fullscreen mode

Output

5
Enter fullscreen mode Exit fullscreen mode

Example

let width = 6;
let height = 8;

console.log(Math.hypot(width, height));
Enter fullscreen mode Exit fullscreen mode

Output

10
Enter fullscreen mode Exit fullscreen mode

These are all some most frequently used methods:

Math.random();       //Generate random values
Math.floor();        //Round down
Math.ceil();         //Round up
Math.round();        //Nearest integer
Math.max();          //Find largest value
Math.min();          //Find smallest value
Math.abs();          //Get positive value
Math.sqrt();         //Square root
Math.pow();          //Exponents
Math.trunc();        //Remove decimals
Enter fullscreen mode Exit fullscreen mode

These are the methods you'll use most often in real-world JavaScript projects.


Reference
https://www.w3schools.com/jS/js_math.asp
https://www.geeksforgeeks.org/javascript/javascript-math-object/
https://www.programiz.com/javascript/library/math

Top comments (0)