Sub topic 10
Meet the Math Object
This chapter assumes you know how JavaScript stores numbers as 64-bit floats, how the arithmetic operators work, and how NaN behaves. You should also be able to run code in a browser console or Node.js.
The arithmetic operators cover +, -, *, /, %, and **. For anything beyond that, such as rounding, square roots, logarithms, or sines, JavaScript gives you the built-in Math object.
Math is a namespace, not a class
Math is a plain object that holds functions and constants. You never create an instance of it. Every member is static, so you call it directly as Math.something.
console.log(typeof Math); // "object"
console.log(Object.prototype.toString.call(Math)); // "[object Math]"
try {
new Math();
} catch (e) {
console.log(e instanceof TypeError); // true: Math is not a constructor
}
console.log(Math.sqrt(16)); // 4Unlike Date, Math has no constructor and holds no state. Each method takes numbers in and returns a number. The one exception is Math.random, which takes no input.
The constants
Math exposes eight read-only constants, all stored as doubles.
| Constant | Meaning | Approximate value |
|---|---|---|
| `Math.PI` | Ratio of a circle's circumference to its diameter | 3.141592653589793 |
| `Math.E` | Euler's number, base of natural logarithms | 2.718281828459045 |
| `Math.LN2` | Natural log of 2 | 0.6931471805599453 |
| `Math.LN10` | Natural log of 10 | 2.302585092994046 |
| `Math.LOG2E` | Base-2 log of e | 1.4426950408889634 |
| `Math.LOG10E` | Base-10 log of e | 0.4342944819032518 |
| `Math.SQRT2` | Square root of 2 | 1.4142135623730951 |
| `Math.SQRT1_2` | Square root of 1/2 | 0.7071067811865476 |
The constants are properties, not functions. Writing Math.PI() throws a TypeError. You can't change them either:
function tryToChangePi() {
"use strict";
try {
Math.PI = 3;
} catch (e) {
console.log(e instanceof TypeError); // true
}
console.log(Math.PI); // 3.141592653589793
}
tryToChangePi();
const circleArea = (r) => Math.PI * r ** 2;
console.log(circleArea(2)); // 12.566370614359172How Math methods treat their arguments
Math methods convert each argument with the same rules as Number(). Bad input therefore gives you NaN, not an exception.
console.log(Math.sqrt("16")); // 4 (string coerced)
console.log(Math.floor(null)); // 0 (null becomes 0)
console.log(Math.floor(undefined)); // NaN
console.log(Math.abs([-5])); // 5 (array -> "-5" -> -5)
console.log(Math.abs([1, 2])); // NaN
console.log(Math.sqrt()); // NaN (missing argument)This leads to the most common Math bug: unvalidated input turns into NaN, and the NaN quietly spreads through later calculations. Validate input before you pass it to Math. A later chapter covers the Number.is* checks.
A map of what's inside
The following chapters cover each family in turn. Math also has some specialist methods: Math.fround rounds to 32-bit float precision, Math.imul does 32-bit integer multiplication, and Math.clz32 counts leading zero bits. You will rarely need them outside graphics or low-level numeric code.
Exercise
What does each line log, and why? (1) Math.max("7", 3) (2) Math.sqrt("nine") (3) typeof Math.E
Show answer
(1) 7, because "7" is coerced to the number 7 before comparing. (2) NaN, because Number("nine") is NaN and Math methods return NaN instead of throwing. (3) "number", because Math.E is a constant property, not a function.
Exercise
A colleague writes const m = new Math(); m.sqrt(9);. What goes wrong, and what is the fix?
Show answer
Math is not a constructor, so new Math() throws a TypeError. Call the static method directly: Math.sqrt(9), which returns 3.