19+ Most Useful JavaScript Math Functions
JavaScript provides a variety of built-in Math functions that help in performing mathematical calculations and operations. These functions are a part of the Math object, which provides tools for handling numerical values, performing mathematical operations, and working with numbers. In this article, we will cover 20+ most useful JavaScript Math functions with examples and outputs to demonstrate their behavior and usage.
01. Math.abs()
The Math.abs()
method returns the absolute value of a number, removing any negative sign.
let number = -5;
let absValue = Math.abs(number);
console.log(absValue); // Output: 5
02. Math.ceil()
The Math.ceil()
method returns the smallest integer greater than or equal to the given number (rounds up).
let number = 5.2;
let ceilValue = Math.ceil(number);
console.log(ceilValue); // Output: 6
03. Math.floor()
The Math.floor()
method returns the largest integer less than or equal to the given number (rounds down).
let number = 5.8;
let floorValue = Math.floor(number);
console.log(floorValue); // Output: 5
04. Math.round()
The Math.round()
method rounds a number to the nearest integer. If the fractional part is 0.5 or higher, the number is rounded up.
let number = 5.5;
let roundedValue = Math.round(number);
console.log(roundedValue); // Output: 6
05. Math.trunc()
The Math.trunc()
method returns the integer part of a number by removing the decimal part (without rounding).
let number = 5.9;
let truncatedValue = Math.trunc(number);
console.log(truncatedValue); // Output: 5
06. Math.max()
The Math.max()
method returns the largest of the zero or more numbers provided as input.
let maxValue = Math.max(1, 5, 3, 9, 7);
console.log(maxValue); // Output: 9
07. Math.min()
The Math.min()
method returns the smallest of the zero or more numbers provided as input.
let minValue = Math.min(1, 5, 3, 9, 7);
console.log(minValue); // Output: 1
08. Math.random()
The Math.random()
method returns a floating-point number between 0 (inclusive) and 1 (exclusive), which can be used for generating random numbers.
let randomValue = Math.random();
console.log(randomValue); // Output: Random number between 0 and 1
09. Math.pow()
The Math.pow()
method returns the base to the exponent power, i.e., baseexponent
.
let powerValue = Math.pow(2, 3);
console.log(powerValue); // Output: 8 (2^3)
10. Math.sqrt()
The Math.sqrt()
method returns the square root of a number. If the number is negative, it returns NaN
.
let sqrtValue = Math.sqrt(16);
console.log(sqrtValue); // Output: 4
11. Math.sin()
The Math.sin()
method returns the sine of a number (in radians).
let sineValue = Math.sin(Math.PI / 2); // sine of 90 degrees (PI/2 radians)
console.log(sineValue); // Output: 1
12. Math.cos()
The Math.cos()
method returns the cosine of a number (in radians).
let cosineValue = Math.cos(Math.PI); // cosine of 180 degrees (PI radians)
console.log(cosineValue); // Output: -1
13. Math.tan()
The Math.tan()
method returns the tangent of a number (in radians).
let tangentValue = Math.tan(Math.PI / 4); // tangent of 45 degrees (PI/4 radians)
console.log(tangentValue); // Output: 1
14. Math.log()
The Math.log()
method returns the natural logarithm (base e
) of a number. For base 10, use Math.log10()
.
let logValue = Math.log(10);
console.log(logValue); // Output: 2.302585092994046
15. Math.exp()
The Math.exp()
method returns ex
, where e
is Euler's number (approximately 2.71828) and x
is the argument.
let expValue = Math.exp(2);
console.log(expValue); // Output: 7.3890560989306495
16. Math.clz32()
The Math.clz32()
method returns the number of leading zero bits in the 32-bit binary representation of a number.
let clzValue = Math.clz32(5); // 5 in binary is 00000000000000000000000000000101
console.log(clzValue); // Output: 29
17. Math.imul()
The Math.imul()
method returns the result of multiplying two 32-bit integers, handling overflow correctly.
let imulValue = Math.imul(1000, 1000);
console.log(imulValue); // Output: 259200000
18. Math.fround()
The Math.fround()
method returns the nearest single-precision float representation of a number.
let froundValue = Math.fround(5.5);
console.log(froundValue); // Output: 5.5
19. Math.sign()
The Math.sign()
method returns the sign of a number, indicating whether the number is positive, negative, or zero.
let signValue = Math.sign(-10);
console.log(signValue); // Output: -1 (negative number)
20. Math.cbrt()
The Math.cbrt()
method returns the cube root of a number.
let cbrtValue = Math.cbrt(27);
console.log(cbrtValue); // Output: 3
Conclusion
JavaScript offers a comprehensive set of Math functions that can help with basic and advanced mathematical calculations. By mastering these functions, you can perform tasks such as rounding numbers, working with trigonometric functions, generating random values, and more. Understanding how and when to use these functions can make your JavaScript code cleaner, more efficient, and capable of handling a variety of mathematical operations with ease.
Comments
Post a Comment