JavaScript Math: Operators and Math Methods

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); // 5

Arithmetic operators Top ↑

Use +, -, *, /, %, 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);  // 32

For the remainder after division, see the remainder (%) operator.

The JavaScript Math object Top ↑

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)); // 12

Rounding methods Top ↑

Math.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));  // 6

Powers, roots and logarithms Top ↑

Use 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)); // 1

Trigonometry Top ↑

JavaScript trigonometric methods use radians. See Math.sin() for degree-to-radian conversion and sine examples.

Random numbers Top ↑

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.

Number parsing and formatting Top ↑

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.

Floating-point precision Top ↑

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"

Math demos and exercises Top ↑

Try the calculator demo, feet-to-meter converter, and inches-to-centimeters converter. Practice with factors, Fibonacci series, prime numbers, factorial, and prime checking.




Subscribe to our YouTube Channel here



plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer