Skip to main content

Archive

Show more

JavaScript Math Object

JavaScript Math Object

The JavaScript Math object provides various properties and methods for mathematical operations. It includes functions for performing basic arithmetic, working with trigonometric functions, and more advanced calculations. This article will cover the key features of the Math object and how to use them.


1. Math.PI

The Math.PI property returns the value of π (pi), which is approximately 3.14159.

Example:

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

2. Math.E

The Math.E property returns the base of the natural logarithm, e, which is approximately 2.71828.

Example:

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

3. Math.sqrt(x)

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

Example:

console.log(Math.sqrt(16));  // Output: 4
console.log(Math.sqrt(-4));  // Output: NaN

4. Math.pow(base, exponent)

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

Example:

console.log(Math.pow(2, 3));  // Output: 8

5. Math.round(x)

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

Example:

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

6. Math.floor(x)

The Math.floor() method returns the largest integer less than or equal to a given number.

Example:

console.log(Math.floor(4.7));  // Output: 4

7. Math.ceil(x)

The Math.ceil() method returns the smallest integer greater than or equal to a given number.

Example:

console.log(Math.ceil(4.1));  // Output: 5

8. Math.max(...values)

The Math.max() method returns the largest of zero or more numbers.

Example:

console.log(Math.max(1, 3, 5, 2));  // Output: 5

9. Math.min(...values)

The Math.min() method returns the smallest of zero or more numbers.

Example:

console.log(Math.min(1, 3, 5, 2));  // Output: 1

10. Math.random()

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

Example:

console.log(Math.random());  // Output: A random number between 0 and 1

11. Math.trunc(x)

The Math.trunc() method returns the integer part of a number by removing any fractional digits.

Example:

console.log(Math.trunc(4.9));  // Output: 4
console.log(Math.trunc(-4.9));  // Output: -4

12. Math.sign(x)

The Math.sign() method returns the sign of the number, indicating whether the number is positive, negative, or zero.

Example:

console.log(Math.sign(10));  // Output: 1
console.log(Math.sign(-10));  // Output: -1
console.log(Math.sign(0));   // Output: 0

13. Math.log(x)

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

Example:

console.log(Math.log(1));   // Output: 0
console.log(Math.log(Math.E));  // Output: 1

14. Math.exp(x)

The Math.exp() method returns e (the base of natural logarithms) raised to the power of a number.

Example:

console.log(Math.exp(1));   // Output: 2.718281828459045

15. Math.cos(x)

The Math.cos() method returns the cosine of a number (angle in radians).

Example:

console.log(Math.cos(Math.PI));   // Output: -1

16. Math.sin(x)

The Math.sin() method returns the sine of a number (angle in radians).

Example:

console.log(Math.sin(Math.PI / 2));  // Output: 1

17. Math.tan(x)

The Math.tan() method returns the tangent of a number (angle in radians).

Example:

console.log(Math.tan(Math.PI / 4));  // Output: 1

Conclusion

The Math object in JavaScript provides a wide range of methods and properties for performing mathematical calculations. Understanding these methods allows you to handle complex calculations, perform rounding, and work with mathematical constants and functions efficiently.

Comments