Language: EN

javascript-biblioteca-math

Math Library Methods in JavaScript

In JavaScript, the Math library is a built-in object in the language itself that provides a wide range of mathematical functions.

This library is not an object created from a constructor but a global object used to perform common and advanced mathematical calculations.

Let’s look at its methods and how we can use them 👇

Properties of the Math Object

PropertyDescription
Math.EThe base of the natural logarithm (approximately 2.718)
Math.PIThe value of π (approximately 3.14159)
Math.SQRT2The square root of 2 (approximately 1.414)
Math.SQRT1_2The square root of 1/2 (approximately 0.707)
Math.LN2The natural logarithm of 2 (approximately 0.693)
Math.LN10The natural logarithm of 10 (approximately 2.303)
Math.LOG2EThe base 2 logarithm of E (approximately 1.442)
Math.LOG10EThe base 10 logarithm of E (approximately 0.434)

Math.PI

The constant Math.PI represents the value of π (pi), which is approximately 3.14159. It is used for calculations involving circles and spheres.

console.log(Math.PI); // 3.141592653589793

Math.E

The constant Math.E represents the base of natural logarithms, approximately 2.71828. It is essential in calculations related to logarithms and exponential growth.

console.log(Math.E); // 2.718281828459045

Math.LN2, Math.LN10, Math.LOG2E, Math.LOG10E

These constants provide common logarithmic values:

  • Math.LN2: The natural logarithm of 2.
  • Math.LN10: The natural logarithm of 10.
  • Math.LOG2E: The base 2 logarithm of e.
  • Math.LOG10E: The base 10 logarithm of e.
console.log(Math.LN2); // 0.6931471805599453
console.log(Math.LOG10E); // 0.4342944819032518

Math.SQRT2, Math.SQRT1_2

These constants represent useful values for advanced calculations and handling edge values:

  • Math.SQRT2: The square root of 2.
  • Math.SQRT1_2: The square root of 1/2.

Methods of the Math Object

Rounding Methods

MethodDescription
Math.round()Rounds the number to the nearest integer.
Math.floor()Rounds the number down to the nearest integer.
Math.ceil()Rounds the number up to the nearest integer.
Math.trunc()Removes the decimal part of the number, returning only the integer part.

Math.round()

The Math.round() method rounds a number to the nearest integer. If the number is halfway between two integers, it rounds towards the nearest positive integer.

console.log(Math.round(4.7)); // 5
console.log(Math.round(4.4)); // 4

Math.ceil()

The Math.ceil() method rounds a number up to the nearest integer.

console.log(Math.ceil(4.1)); // 5
console.log(Math.ceil(-4.1)); // -4

Math.floor()

The Math.floor() method rounds a number down to the nearest integer.

console.log(Math.floor(4.9)); // 4
console.log(Math.floor(-4.9)); // -5

Mathematical Operation Methods

MethodDescription
Math.sqrt()Returns the square root of a number
Math.pow()Returns the base raised to the exponent
Math.abs()Returns the absolute value of a number
Math.cbrt()Returns the cube root of a number
Math.max()Returns the largest number
Math.min()Returns the smallest number

Math.sqrt()

The Math.sqrt() method returns the square root of a number. If the number is negative, it returns NaN.

console.log(Math.sqrt(16)); // 4
console.log(Math.sqrt(25)); // 5

Math.pow()

The Math.pow() method returns the base raised to the power of the given exponent.

console.log(Math.pow(2, 3)); // 8
console.log(Math.pow(5, 2)); // 25

Math.abs()

The Math.abs() method returns the absolute value of a number, that is, its value without the sign.

console.log(Math.abs(-5)); // 5
console.log(Math.abs(5)); // 5

Generation Methods

MethodDescription
Math.random()Returns a pseudo-random number

Math.random()

The Math.random() method returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random()); // Example: 0.7321357898475432
console.log(Math.random()); // Example: 0.2498305234657335

Trigonometry Methods

MethodDescription
Math.sin()Returns the sine of an angle
Math.cos()Returns the cosine of an angle
Math.tan()Returns the tangent of an angle
Math.asin()Returns the arcsine of a number
Math.acos()Returns the arccosine of a number
Math.atan()Returns the arctangent of a number
Math.atan2()Returns the arctangent of the coordinates

Math.sin(), Math.cos(), Math.tan()

These methods return the sine, cosine, and tangent of a given angle in radians.

console.log(Math.sin(Math.PI / 2)); // 1
console.log(Math.cos(Math.PI)); // -1
console.log(Math.tan(Math.PI / 4)); // 1

Math.asin(), Math.acos(), Math.atan()

These methods return the arcsine, arccosine, and arctangent of a number, resulting in an angle in radians.

console.log(Math.asin(1)); // 1.5707963267948966 (PI/2)
console.log(Math.acos(-1)); // 3.141592653589793 (PI)
console.log(Math.atan(1)); // 0.7853981633974483 (PI/4)

Logarithm Methods

MethodDescription
Math.log()Returns the natural logarithm (base e) of a number
Math.log10()Returns the base 10 logarithm of a number
Math.log2()Returns the base 2 logarithm of a number
Math.exp()Returns e raised to the power of a number

Math.exp()

The Math.exp() method returns the value of e raised to the power of the given number.

console.log(Math.exp(1)); // 2.718281828459045
console.log(Math.exp(2)); // 7.3890560989306495

Math.log()

The Math.log() method returns the natural logarithm of a number.

console.log(Math.log(Math.E)); // 1
console.log(Math.log(10)); // 2.302585092994046

Sign and Comparison Methods

MethodDescription
Math.sign()Returns the sign of a number: 1 for positive, -1 for negative, and 0 for zero.