JavaScript Math object : Mathematical operations

The Math object provides a number of mathematical functions that can be used in calculations. Consider the basic mathematical functions.

abs()

The function abs()returns the absolute value of a number:

var x = -25;
document.write(Math.abs(x)); // 25
var y = 34;
document.write(Math.abs(y)); // 34

min() and max()

The min() and functions max() return the minimum and maximum value from a set of numbers, respectively:

var max = Math.max(19, 45); // 45
var min = Math.min(33, 24); // 24

These functions do not have to accept two numbers, but more numbers can be passed to them:

var max = Math.max(1, 2, 3, -9, 46, -23); // 46

ceil()

The function ceil() rounds a number up to the next largest integer:

var x = Math.ceil(9.2); // ten
var y = Math.ceil(-5.9); // -5

The expression Math.ceil(9.2)returns 10 because 10 is the next largest integer after 9.2. And also the expression Math.ceil(-5.9)returns -5 because -5 is the next largest integer after -5.9

floor()

The function floor() rounds a number down to the next smallest integer:

var x = Math.floor(9.2); // 9
var y = Math.floor(-5.9); // -6

round()

The function round() rounds a number down to the next smallest integer if its decimal part is less than 0.5. If the decimal part is equal to or greater than 0.5, then rounding goes to the nearest largest integer:

var x = Math.round(5.5); // 6
var y = Math.round(5.4); // 5
varz = Math.round(-5.4); // -5
varn = Math.round(-5.5); // -5
var m = Math.round(-5.6); // -6
console log(x);
console log(y);
console log(z);
console log(n);

random()

The function random() returns a random floating point number of their range from 0 to 1:

var x = Math.random();

pow()

The function pow() returns a number to a certain extent. For example, let’s raise the number 2 to the power of 3:

var x = Math.pow(2, 3); // eight

sqrt()

The function sqrt() returns the square root of a number:

var x = Math.sqrt(121); // eleven
var y = Math.sqrt(9); // 3
varz = Math.sqrt(20); // 4.47213595499958

log()

The function log() returns the natural logarithm of a number:

var x = Math.log(1); // 0
varz = Math.log(10); // 2.302585092994046

Trigonometric functions

A number of functions represent trigonometric functions: sin()(calculates the sine of an angle), cos()(calculates the cosine of an angle), tan()(calculates the tangent of an angle).

var x = Math.sin(90); // 0.8939966636005579
var y = Math.cos(0); // 1
var z = Math.tan(45); // 1.6197751905438615

The function asin()calculates the arcsine of a number, the acosarccosine, and atan()the arctangent of the number:

var x = Math.asin(0.9); // 1.1197695149986342
var y = Math.acos(1); // 1
var z = Math.atan(1); // 0.7853981633974483

Constants

In addition to methods, the Math object also defines a set of built-in constants that can be used in various calculations:

  • Math.PI(PI number): 3.141592653589793
  • Math.SQRT2(square root of two): 1.4142135623730951
  • Math.SQRT1_2(half of the square root of two): 0.7071067811865476
  • Math.E(number e or Euler number): 2.718281828459045
  • Math.LN2(natural logarithm of the number 2): 0.6931471805599453
  • Math.LN10(natural logarithm of 10): 2.302585092994046
  • Math.LOG2E(binary logarithm of e): 1.4426950408889634
  • Math.LOG10E(decimal logarithm of e): 0.4342944819032518

We use constants in calculations:

var x = Math.log(Math.E); // one
varz = Math.tan(Math.PI/4); // 0.9999999999999999