← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 2 of 26

JS Math

Think of a scientific calculator built right into your programming language, always available, never needing a separate app, ready to round numbers, find the biggest value, or generate a random pick. JavaScript's built-in Math object is exactly that calculator, a collection of ready-made methods for common numeric tasks so you never have to write rounding or randomization logic completely from scratch. From displaying a rounded quiz score to picking a random featured tutorial on cookiescursor.com, the Math object quietly handles a lot of everyday numeric work.
Syntax
javascript
Math.round(number);
Math.random();
Math.max(a, b);
Math.floor(number);
Math.PI

Rounding: round, floor, and ceil

Math.round() rounds to the nearest whole number, Math.floor() always rounds down, and Math.ceil() always rounds up, regardless of how close the decimal is to the next whole number.

Note: Use Math.floor() when calculating something like 'how many complete groups fit', since it never rounds up past what actually fits.
Warning: Math.round() rounds exactly .5 upward, which can be surprising for negative numbers, where it behaves slightly differently than expected.

उदाहरण: Rounding: round, floor, and ceil

javascript
// Print `Math.round(4.5)` to the console
// Print `Math.round(4.5)` to the console
console.log(Math.round(4.5));
// Print `Math.floor(4.9)` to the console
// Print `Math.floor(4.9)` to the console
console.log(Math.floor(4.9));
// Print `Math.ceil(4.1)` to the console
// Print `Math.ceil(4.1)` to the console
console.log(Math.ceil(4.1));

abs, max, and min

Math.abs() returns a number's absolute value, always positive, Math.max() returns the largest of a set of values, and Math.min() returns the smallest.

All three accept any number of arguments and are commonly combined with the spread operator to work on array contents.

Note: Use the spread operator with an array, like Math.max(...scores), to find the maximum value directly from an array of numbers.
Warning: Math.max() and Math.min() called with zero arguments return -Infinity and Infinity respectively, which can silently break comparisons on an empty list.

उदाहरण: abs, max, and min

javascript
// Print `Math.abs(-5)` to the console
// Print `Math.abs(-5)` to the console
console.log(Math.abs(-5));
// Print `Math.max(3, 7, 2)` to the console
// Print `Math.max(3, 7, 2)` to the console
console.log(Math.max(3, 7, 2));
// Print `Math.min(3, 7, 2)` to the console
// Print `Math.min(3, 7, 2)` to the console
console.log(Math.min(3, 7, 2));

Math.random()

Math.random() returns a random decimal number between 0 (inclusive) and 1 (exclusive), which is typically scaled and rounded to produce a random whole number within a specific range.

Note: The common formula Math.floor(Math.random() * (max - min + 1)) + min reliably generates a random whole number between min and max, inclusive.
Warning: Math.random() is not cryptographically secure, avoid it for anything security sensitive, like generating tokens or passwords.

उदाहरण: Math.random()

javascript
console.log(Math.random()); // decimal between 0 and 1
const diceRoll = Math.floor(Math.random() * 6) + 1;
console.log(diceRoll);

pow and sqrt

Math.pow(base, exponent) raises a number to a power, functionally equivalent to the ** operator, and Math.sqrt() calculates a number's square root.

Math.sqrt() of a negative number returns NaN, since JavaScript's Math object only works with real numbers.

Note: In modern code, the ** operator is generally preferred over Math.pow() for its more concise syntax, though both produce identical results.
Warning: Math.sqrt() of a negative number returns NaN, not an error, so it's worth checking a value's sign before calling it if negatives are possible.

उदाहरण: pow and sqrt

javascript
console.log(Math.pow(2, 3));
console.log(Math.sqrt(16));
console.log(Math.sqrt(-4)); // NaN

Math.PI and Other Constants

Math.PI provides the mathematical constant pi with full floating-point precision, useful for geometry calculations like circle area or circumference, without needing to type or remember its digits manually.

Note: Using Math.PI instead of hardcoding an approximation like 3.14 ensures your calculations remain as precise as JavaScript's number type allows.
Warning: Math.PI is a read-only property, not a function, it's accessed without parentheses, Math.PI, not Math.PI().

उदाहरण: Math.PI and Other Constants

javascript
console.log(Math.PI);
const radius = 5;
console.log(Math.PI * radius * radius); // circle area
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.