Science Knowings: JavaScript Course For Social Media

Math Object

Math Object

The Math object provides a set of built-in mathematical functions and properties for performing complex calculations in JavaScript.

Math Properties

PropertyDescription
EEuler's number (approximately 2.718)
LN10Natural logarithm (base e) of 10
LN2Natural logarithm (base e) of 2
LOG2EBase 2 logarithm of e
LOG10EBase 10 logarithm of e
PIThe ratio of a circle's circumference to its diameter (approximately 3.14159)
SQRT1_2Square root of 1/2
SQRT2Square root of 2

Math Methods

The Math object provides various methods for performing mathematical operations:

abs()

Returns the absolute value of a number (removes the negative sign if present).

Math.abs(-5); // 5

acos()

Returns the arccosine (inverse cosine) of a number (in radians).

Math.acos(0.5); // 1.0471975511965976

asin()

Returns the arcsine (inverse sine) of a number (in radians).

Math.asin(0.5); // 0.5235987755982988

atan()

Returns the arctangent (inverse tangent) of a number (in radians).

Math.atan(1); // 0.7853981633974483

atan2()

Returns the arctangent of the quotient of two numbers (in radians). Useful for calculating angles in trigonometry.

Math.atan2(3, 4); // 0.6435011087932844

ceil()

Returns the smallest integer greater than or equal to a number.

Math.ceil(4.2); // 5

cos()

Returns the cosine of a number (in radians).

Math.cos(Math.PI / 2); // 0

exp()

Returns the exponential of a number (e raised to the power of the given number).

Math.exp(2); // 7.38905609893065

floor()

Returns the largest integer less than or equal to a number.

Math.floor(4.9); // 4

log()

Returns the natural logarithm (base e) of a number.

Math.log(10); // 2.302585092994046

max()

Returns the largest of zero or more numbers.

Math.max(1, 3, 5); // 5

min()

Returns the smallest of zero or more numbers.

Math.min(1, 3, 5); // 1

pow()

Returns the power of a number (base raised to the power of the exponent).

Math.pow(2, 3); // 8

random()

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

Math.random(); // 0.123456789

round()

Returns the value of a number rounded to the nearest integer.

Math.round(3.14); // 3

sin()

Returns the sine of a number (in radians).

Math.sin(Math.PI / 2); // 1

sqrt()

Returns the square root of a number.

Math.sqrt(9); // 3

tan()

Returns the tangent of a number (in radians).

Math.tan(Math.PI / 4); // 1

trunc()

Returns the integer part of a number by removing any fractional digits.

Math.trunc(3.14); // 3

Next Topic: DOM (Document Object Model)

The DOM (Document Object Model) provides a structured representation of the content, structure, and styling of a web page. By learning the DOM, you'll gain control over elements in a web page, making it interactive and dynamic. Follow us for more!