Categories: softare development

Complete JavaScript Math Reference

The Math object in JavaScript is a built-in object that provides mathematical constants and functions.
It’s not a constructor, so you don’t use new Math().
You access methods like this:

Learn how to build softwares

Math.methodName(arguments)

1. Math Constants

console.log(Math.E);        // 2.718281828459045 (Euler's number)
console.log(Math.PI);       // 3.141592653589793 (π)
console.log(Math.SQRT2);    // 1.4142135623730951 (√2)
console.log(Math.SQRT1_2);  // 0.7071067811865476 (√1/2)
console.log(Math.LN2);      // 0.6931471805599453 (ln 2)
console.log(Math.LN10);     // 2.302585092994046 (ln 10)
console.log(Math.LOG2E);    // 1.4426950408889634 (log2(e))
console.log(Math.LOG10E);   // 0.4342944819032518 (log10(e))

2. Basic Math Methods

console.log(Math.abs(-7));      // 7 → absolute value
console.log(Math.sign(-10));    // -1 → negative
console.log(Math.sign(0));      // 0
console.log(Math.sign(8));      // 1 → positive
console.log(Math.trunc(5.89));  // 5 → removes decimals
console.log(Math.round(4.6));   // 5 → rounds to nearest integer
console.log(Math.ceil(4.1));    // 5 → rounds up
console.log(Math.floor(4.9));   // 4 → rounds down

3. Power & Roots

console.log(Math.pow(2, 3));    // 8 → 2³
console.log(Math.sqrt(16));     // 4 → √16
console.log(Math.cbrt(27));     // 3 → ∛27
console.log(Math.hypot(3, 4));  // 5 → √(3² + 4²)

4. Exponential & Logarithmic

console.log(Math.exp(1));       // 2.718281828459045 → e¹
console.log(Math.expm1(1));     // 1.718281828459045 → e¹ - 1
console.log(Math.log(Math.E));  // 1 → natural log (base e)
console.log(Math.log10(100));   // 2 → base 10 log
console.log(Math.log2(8));      // 3 → base 2 log
console.log(Math.log1p(0.5));   // 0.4054651081081644 → log(1 + x)

5. Trigonometric Functions

console.log(Math.sin(Math.PI / 2));  // 1 → sin(90°)
console.log(Math.cos(0));            // 1 → cos(0°)
console.log(Math.tan(Math.PI / 4));  // 1 → tan(45°)
console.log(Math.asin(1));           // 1.5707963267948966 → arcsin(1) = π/2
console.log(Math.acos(0));           // 1.5707963267948966 → arccos(0) = π/2
console.log(Math.atan(1));           // 0.7853981633974483 → arctan(1) = π/4
console.log(Math.atan2(1, 1));       // 0.7853981633974483 → atan2(y, x)

6. Hyperbolic Functions

console.log(Math.sinh(0));    // 0 → hyperbolic sine
console.log(Math.cosh(0));    // 1 → hyperbolic cosine
console.log(Math.tanh(0));    // 0 → hyperbolic tangent
console.log(Math.asinh(1));   // 0.881373587019543 → inverse hyperbolic sine
console.log(Math.acosh(1));   // 0 → inverse hyperbolic cosine
console.log(Math.atanh(0.5)); // 0.5493061443340549 → inverse hyperbolic tangent

7. Random Numbers

console.log(Math.random()); 
// Random number between 0 (inclusive) and 1 (exclusive)

console.log(Math.floor(Math.random() * 10)); 
// Random integer between 0 and 9

console.log(Math.floor(Math.random() * (20 - 10 + 1)) + 10);
// Random integer between 10 and 20

8. Min, Max, and Clamping

console.log(Math.max(1, 5, 10, 3));  // 10 → largest number
console.log(Math.min(1, 5, 10, 3));  // 1 → smallest number

// Clamping a number between 0 and 100:
let value = 120;
let clamped = Math.min(Math.max(value, 0), 100);
console.log(clamped); // 100

9. Miscellaneous / Utility Functions

console.log(Math.imul(3, 4));        // 12 → integer multiplication (32-bit)
console.log(Math.fround(1.337));     // 1.3370000123977661 → 32-bit float
console.log(Math.clz32(1));          // 31 → count leading zeros in 32-bit binary
console.log(Math.atan2(10, 10));     // 0.7853981633974483 → angle in radians

10. Example: Using Math in Real Use Cases

Area of a circle

let radius = 5;
let area = Math.PI * Math.pow(radius, 2);
console.log(area); // 78.53981633974483

Random password length generator (8–16 chars)

let length = Math.floor(Math.random() * (16 - 8 + 1)) + 8;
console.log(length);

Distance between two points (x₁, y₁) and (x₂, y₂)

let x1 = 2, y1 = 3, x2 = 7, y2 = 8;
let distance = Math.hypot(x2 - x1, y2 - y1);
console.log(distance); // 7.0710678118654755

Convert degrees to radians

let degrees = 180;
let radians = degrees * (Math.PI / 180);
console.log(radians); // 3.141592653589793

Summary

CategoryCommon Methods
ConstantsE, PI, SQRT2, SQRT1_2, LN2, LN10, LOG2E, LOG10E
Roundingabs, sign, trunc, round, ceil, floor
Power/Rootspow, sqrt, cbrt, hypot
Exponential/Logsexp, expm1, log, log10, log2, log1p
Trigonometrysin, cos, tan, asin, acos, atan, atan2
Hyperbolicsinh, cosh, tanh, asinh, acosh, atanh
Random/Utilityrandom, min, max, fround, imul, clz32
Share
Published by
codeflare

Recent Posts

The Golden Ratio (φ)

1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…

4 days ago

CSS Combinators

In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based…

6 days ago

Boolean Algebra

Below is a comprehensive, beginner-friendly, yet deeply detailed guide to Boolean Algebra, complete with definitions, laws,…

1 week ago

Why It’s Difficult to Debug Other People’s Code (And what Can be Done About it)

Debugging your own code is hard enough — debugging someone else’s code is a whole…

1 week ago

Complete Git Commands

Git is a free, open-source distributed version control system created by Linus Torvalds.It helps developers: Learn how to…

2 weeks ago

Bubble Sort Algorithm

Bubble Sort is one of the simplest sorting algorithms in computer science. Although it’s not…

2 weeks ago