JavaScript Math Functions: Bounds, Powers and Distances
Quick answer
Math is a namespace of numerical operations and constants. Choose a function that expresses the actual numerical problem rather than manually reproducing its calculation.
How it works
Math.min and Math.max accept individual numbers; with no arguments they return Infinity and -Infinity respectively. Math.abs returns magnitude, Math.sqrt computes a square root, Math.pow raises a base to an exponent and Math.hypot computes Euclidean length. Negative real square roots yield NaN. Trigonometric functions use radians. Math.random is nondeterministic and not cryptographically secure; it is deferred to a dedicated reference.
console.log(Math.min(8,2,5), Math.max(8,2,5), Math.abs(-7));Expected output in Node.js:
2 8 7Min and max select bounds; abs removes the sign.
console.log(Math.hypot(3,4), Math.sqrt(81), Math.pow(2,5));Expected output in Node.js:
5 9 32A 3-4 right triangle has length 5; the other operations compute roots and powers.
Common mistakes
Pass separate numbers or use spread for a small bounded array. An empty list needs a domain-specific policy rather than accidentally treating an infinite bound as a measurement.
Practice
Calculate the length of a displacement using two components.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Math Functions: Bounds, Powers and Distances — Knowledge Check
Apply the taught concept and predict the output.
What is Math.min() with no arguments?
Which Math method computes Euclidean length from component values?
What angle unit is expected by Math.sin?
What is the exact console output in JavaScript Math Functions: Bounds, Powers and Distances example 1?
console.log(Math.min(8,2,5), Math.max(8,2,5), Math.abs(-7));What is the exact console output in JavaScript Math Functions: Bounds, Powers and Distances example 2?
console.log(Math.hypot(3,4), Math.sqrt(81), Math.pow(2,5));