Home >Web Front-end >JS Tutorial >Useful JavaScript Math Functions and How to Use Them
This article explores JavaScript's built-in Math
object, a treasure trove of functions for various mathematical operations. We'll examine key functions and their practical applications.
Key Concepts
This guide covers:
Math
Object: A comprehensive overview of the Math
object and its functions for performing mathematical calculations. We'll demonstrate the use of functions like Math.max
, Math.min
, Math.abs
, Math.pow
, Math.sqrt
, and Math.hypot
in diverse programming contexts.Math.sqrt
, Math.cbrt
, Math.log
, Math.log2
, Math.log10
, and Math.hypot
will be explained in detail.Math.max
and Math.min
These functions return the maximum and minimum values from a list of numerical arguments. Non-numeric arguments result in NaN
. JavaScript attempts type coercion; for example, true
is coerced to 1
. Arrays can be handled using the spread operator (...
).
<code class="language-javascript">Math.max(1, 2, 3, 4, 5); // 5 Math.min(4, 71, -7, 2, 1, 0); // -7 Math.max(...[8, 4, 2, 1]); // 8</code>
Example: Finding the high score from an array:
<code class="language-javascript">const scores = [23, 12, 52, 6, 25, 38, 19, 37, 76, 54, 24]; const highScore = Math.max(...scores); // 76</code>
Absolute Values (Math.abs
)
Math.abs
returns the absolute value (magnitude) of a number.
<code class="language-javascript">Math.abs(5); // 5 Math.abs(-42); // 42 Math.abs(-3.14159); // 3.14159</code>
Example: Calculating savings between two deals:
<code class="language-javascript">const dealA = 150; const dealB = 167; const saving = Math.abs(dealA - dealB); // 17</code>
Power Calculations (Math.pow
and the Exponentiation Operator)
Math.pow
calculates powers (base raised to the exponent). The exponentiation operator (**
) provides equivalent functionality.
<code class="language-javascript">Math.pow(2, 3); // 8 2 ** 3; // 8</code>
Root Calculations (Math.sqrt
, Math.cbrt
)
Math.sqrt
calculates the square root, while Math.cbrt
calculates the cube root. Both attempt type coercion.
<code class="language-javascript">Math.sqrt(4); // 2 Math.cbrt(1000); // 10</code>
Other roots can be calculated using fractional exponents:
<code class="language-javascript">625 ** 0.25; // 5 (fourth root)</code>
Logarithms (Math.log
, Math.log2
, Math.log10
)
Math.log
(natural logarithm, base e), Math.log2
(base 2), and Math.log10
(base 10) calculate logarithms.
<code class="language-javascript">Math.max(1, 2, 3, 4, 5); // 5 Math.min(4, 71, -7, 2, 1, 0); // -7 Math.max(...[8, 4, 2, 1]); // 8</code>
Hypotenuse Calculation (Math.hypot
)
Math.hypot
calculates the hypotenuse of a right-angled triangle (shortest distance between two points).
<code class="language-javascript">const scores = [23, 12, 52, 6, 25, 38, 19, 37, 76, 54, 24]; const highScore = Math.max(...scores); // 76</code>
Example: Calculating the distance between two points on a page:
<code class="language-javascript">Math.abs(5); // 5 Math.abs(-42); // 42 Math.abs(-3.14159); // 3.14159</code>
This comprehensive guide provides a solid foundation for utilizing the Math
object's capabilities in your JavaScript projects. Remember to consult the official JavaScript documentation for the most up-to-date information and details.
The above is the detailed content of Useful JavaScript Math Functions and How to Use Them. For more information, please follow other related articles on the PHP Chinese website!