JavaScript Math


Math object

The Math object does not have a constructor, unlike other objects. The math item has no movement. You can use each method and property without first constructing a Math object.


Math properties

Any Math property has the following syntax: math.property. Eight mathematical constants are available in JavaScript and may be accessed using Math properties.

Example

Preview

Math method

Math.method is the syntax for any math method (number).


Number to interger

Four popular techniques exist for rounding numbers to integers:

  1. Math.round()
  2. Math.ceil()
  3. Math.floor()
  4. Math.trunc()

Math.round()

Rounds a number to the nearest integer.

The JavaScript Math object provides the Math.round() function, which rounds a supplied value to the closest integer. The fractional component of the number is rounded down if it is less than 0.5. The fractional component of the number is rounded up if it is 0.5 or higher.

Example

Preview

Math.ceil()

Rounds a number up to the nearest integer.

The Math object in JavaScript has a function called Math.ceil() that rounds numbers up to the closest integer. If the value already has no decimal places, the original number is returned because this technique always rounds up.

Example

Preview

Math.floor()

Rounds a number down to the nearest integer.

The JavaScript Math object offers the Math.floor() function, which reduces a specified value to the closest integer. If the value already has no decimal places, the original number is returned because this technique always rounds down.

Example

Preview

Math.trunc()

Returns the integer part of x.

The Math object in JavaScript provides the Math.trunc() function, which returns the integer portion of a given value after eliminating any fractional digits. The decimal portion of a number is essentially truncated, and only the complete value is returned.

Example

Preview

Math.sign()

Math.sign(x) returns if x is negative, null or positive:

returns a number's sign, which shows whether it is positive, negative, or zero. Returns 1 when the value is positive, -1 when it is negative, and 0 when the value is zero.

Example

Preview

Math.pow()

Math.pow(x, y) returns the value of x to the power of y.

The Math object in JavaScript provides the Math.pow() function, which gives the outcome of raising a given integer to a particular power. The base number and the exponent are the two inputs required by the procedure. The exponent is the power you want to increase the base number to, and the base number is the number you want to raise to a power.

Example

Preview

Math.sqrt()

Returns the square root of a number.

The JavaScript Math object has a function called Math.sqrt() that returns the square root of a specified value. The square root of the given integer is the only argument to the procedure.

Example

Preview

Math.abs()

Math.abs(x) returns the absolute (positive) value of x.

The JavaScript Math object has a function called Math.abs() that returns a number's absolute value. The integer to be used as the parameter in the method is the one that has to be absolute.

Example

Preview

Math.sin()

The sine (a number between -1 and 1) of the angle x is returned by Math.sin(x) (given in radians). You must convert degrees to radians if you wish to utilize degrees instead of radians.

Example

Preview

Math.cos()

Math.cos(x) provides the cosine of the angle x (a number between -1 and 1). (given in radians). You must convert degrees to radians if you wish to utilize degrees instead of radians.

Example

Preview

Math.min() & Math.max()

Math.max(): Returns the highest number in a list of numbers.

Math.min(): Returns the lowest number in a list of numbers.

The Math object in JavaScript has the Math.min() and Math.max() methods, which return the minimum and maximum values from a list of integers.

Example

Preview

Math.random()

Generates a random number between 0 and 1.

A random integer between 0 (inclusive) and 1 is returned by the JavaScript function Math.random() of the Math object (exclusive).

Example

Preview

Math.log()

The natural logarithm of x is returned by math.log(x). The time required to attain a particular degree of growth is given by the natural logarithm. The Math object in JavaScript has a function called Math.log() that returns the natural logarithm (base e) of a specified value.

Example

Preview

Math.log2()

The Math object in JavaScript has a function called Math.log2() that returns a number's base-2 logarithm.

Example

Preview

Math.log10()

The Math object in JavaScript has a function called Math.log10() that returns a number's base-10 logarithm.

Example

Preview