JavaScript provides arithmetic operators for everyday calculations and the built-in Math object for constants and mathematical functions. Math is a static object, so methods are called directly as Math.round(), Math.sqrt(), Math.random(), and similar methods.
const total = 12 + 5;
const rounded = Math.round(4.7);
console.log(total); // 17
console.log(rounded); // 5Use +, -, *, /, %, and ** for addition, subtraction, multiplication, division, remainder, and exponentiation.
console.log(2 + 5); // 7
console.log(4 - 2); // 2
console.log(4 * 9); // 36
console.log(12 / 7); // 1.7142857142857142
console.log(12 % 7); // 5
console.log(2 ** 5); // 32For the remainder after division, see the remainder (%) operator.
Start with Math constants, or jump to Math.max()/Math.min() and Math.abs() for common comparisons and absolute values.
Math contains constants such as Math.PI and static methods for rounding, powers, logarithms, trigonometry, and random values. It is not a constructor, so you do not use new Math().
console.log(Math.PI);
console.log(Math.sqrt(81)); // 9
console.log(Math.abs(-12)); // 12Math.round() selects the nearest integer, Math.floor() rounds toward negative infinity, and Math.ceil() rounds toward positive infinity.
console.log(Math.round(5.6)); // 6
console.log(Math.floor(5.6)); // 5
console.log(Math.ceil(5.1)); // 6Use Math.pow() or ** for powers, Math.sqrt() for square roots, Math.exp() for ex, and Math.log() for natural logarithms.
console.log(Math.pow(2, 8)); // 256
console.log(Math.sqrt(144)); // 12
console.log(Math.log(Math.E)); // 1JavaScript trigonometric methods use radians. See Math.sin() for degree-to-radian conversion and sine examples.
Math.random() returns a pseudo-random number from 0 inclusive up to 1 exclusive. Combine it with Math.floor() for integer ranges. It is not intended for security-sensitive randomness.
Several related tasks belong to the Number API rather than Math. Use parseInt() and parseFloat() for parsing, Number.isInteger() to test integers, and toFixed() or toPrecision() for number formatting. See also NaN checking and NaN behavior.
For base conversion, see number-base conversion and decimal to hexadecimal.
JavaScript Number values use binary floating-point representation, so some decimal calculations cannot be represented exactly. For money and other precision-sensitive work, define a rounding strategy instead of assuming every decimal operation is exact.
console.log(0.1 + 0.2); // 0.30000000000000004
console.log((0.1 + 0.2).toFixed(2)); // "0.30"Try the calculator demo, feet-to-meter converter, and inches-to-centimeters converter. Practice with factors, Fibonacci series, prime numbers, factorial, and prime checking.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.