Javascript Math Object

The Javascript Math Object offers mathematical functions and constants with attributes and methods. Math, unlike all other global objects, does not have a constructor. Math's attributes and methods are entirely static and may be accessed by using Math as an object rather than constructing it.

As a result, you refer to pi as Math.PI and the sine function as Math.sin(x).

Syntax

The syntax for referring to Math methods and properties is as follows.
var pi_val = Math.PI;
var sine_val = Math.sin(30);

Javascript Math Object Properties

Math.property is the syntax for any Javascript Math property.

JavaScript has eight mathematical constants available as Math properties:

  • Euler's number is returned by Math.E.
  • PI is returned by Math.PI.
  • Math.SQRT2 will give back the sq. root of two (2).
  • Math.SQRT12 / calculates the square root of 1/2.
  • Math.LN2 / computes the natural logarithm of 2
  • Math.LN10 / computes the natural logarithm of ten.
  • Math.LOG2E / returns E's base 2 logarithms.
  • Math.LOG10E / returns the logarithm of E in base ten.

Javascript Math Methods

Math.method (No.) is the syntax for Math any methods.

Number to an Integer

There are four commonly used techniques for rounding a number to an integer:

  • Math.round(x) -  x is returned rounded to the closest integer.
  • Math.ceil(x)  -  x is rounded up to the closest integer.
  • Math.floor(x) -  x is rounded down to the nearest integer.
  • Math.trunc(x) - The integer component of x is returned (new in ES6)

Javascript Math Object Max

Math.max() retrieves the biggest number from a collection of numeric values supplied as parameters. Math.max() will give back NaN when a non-numeric value is supplied as a parameter.

Spread (…) or apply can be used to send arrays of numeric values as just a single parameter to Math.max(). However, either of these approaches may fail if the number of array values becomes too large.

Syntax

Math.max(1st value, 2nd value, 3rd value, ...);

Parameters

Numbers or a restricted array of numbers

Return Value

The sum of the supplied numeric numbers, or NaN if any of the given values is non-numeric.

Examples

Numbers As Parameters
Math.max(5, 14, 28, 0, -6); // returns 28

Invalid Parameters
Math.max(5, 14, 28, 'nine', -6); // returns NaN

Array As Parameter, Using Spread(…)
let numbers = [5, 14, 28, 0, -6];
Math.max(…numbers); // returns 28

Array As Parameter, Using Apply
let numbers = [5, 14, 28, 0, -6];
Math.max.apply(null, numbers); // returns 28

Javascript Math Min

Math.min() returns the lowest of zero (0) or more numbers.

Math.min(6, 3, 8, -5);
// returns -5

Javascript Math Pow

Math.pow() gives the value of one number raised to the power of another.

Syntax

Math.pow(exponent, base), where the exponent is the exponent by which the base is raised and the base is simply the base number.

Because pow() is a static function of Math, it is usually referred to as Math instead of as a method on some other object.

Example

Math.pow(7, 3);

Javascript Math Sqrt

The square root of an integer is returned by the Math.sqrt() function.

NaN would be returned if a negative number is supplied.

sqrt() is always referred to as Math because it's a static method.

Syntax

Math.sqrt(y), where "y" is a number.

Javascript Math Trunc

Math.trunc() is a standard object function that returns just the integer component of a given value by deleting fractional units. As a result, the overall rounding is approaching zero. Any non-number input will result in a NAN output.

Note: This isn't supported by older browsers.

Example

Math.trunc(4.8);
Math.trunc(4.6);
Math.trunc(4.3);
Math.trunc(4.1);
Math.trunc(-4.1);

Javascript Math Ceil

Math.ceil() is a standard object method which rounds a supplied number to the next integer. Keep in mind that for negative values, this implies that the integer will be rounded "towards zero" rather than the number with the bigger absolute value.

Example

Math.ceil(0.2) // 1
Math.ceil(1.4) // 2
Math.ceil(-0.8) // -0
Math.ceil(-1.5) // -1

Javascript Math Floor

Math.floor() is a Math standard object function that rounds off a supplied value to the closest integer. Because Math.floor() returns the greatest integer equal to or less than the specified number, negative values would be rounded "away from zero" rather than to the number with the smaller absolute value.

It's the opposite of the Javascript Match Ceil object we highlighted above.

Examples

Math.floor(0.9)  //  0
Math.floor(1.4)  //  1
Math.floor(0.5)  //  0
Math.floor(-0.8) // -1
Math.floor(-1.3) // -2

All Javascript Math Methods and Functions

  • acos() calculates the arccosine of a given integer in radians.
  • asin() calculates the arcsine of a given integer in radians.
  • The arc-tangent of the supplied integer in radians is returned by atan().
  • The cube root of the supplied integer is returned by cbrt().
  • Ceil() provides the absolute smallest integer value that is bigger than or is equal to the specified number.
  • cos() calculates the cosine of the provided number.
  • cosh() calculates the hyperbolic cosine of a given number.
  • exp() returns the provided number in its exponential form.
  • hypot() provides the square root of the sum total of the squares of the provided integers.
  • log (x) returns the natural logarithm of any number.
  • random() gives a random integer between Zero (inclusive) and One (exclusive).
  • round() returns the nearest integer value to the provided number.
  • sign() provides the sign of the given number.
  • sin() calculates the sine of the provided number.
  • sinh() calculates the hyperbolic sine of a given number.
  • tan() calculates the tangent of the provided number.
  • tanh() calculates the hyperbolic tangent of a given integer.
  • trunc() provides an integer portion of the specified number.