← Back to Chapters

JavaScript Math Object

? JavaScript Math Object and Math.random()

⚡ Quick Overview

The Math object in JavaScript provides built-in properties (constants) and methods (functions) for common mathematical operations such as rounding, powers, square roots, absolute values, and random number generation using Math.random(). It is a static utility object, so you use it directly as Math.something, not by creating instances.

? Key Concepts

  • Math is a built-in static object; you never use new Math().
  • Math properties (like Math.PI, Math.E, Math.SQRT2) are constants, not functions.
  • Math methods (like Math.round(), Math.floor(), Math.sqrt(), Math.abs()) perform numeric operations.
  • Math.random() returns a pseudo-random number in the range [0, 1) (0 inclusive, 1 exclusive).
  • You can combine Math.random() with Math.floor() to produce random integers in any range you want.

? Syntax and Theory

The general pattern for using the Math object is:

result = Math.methodName(value);

  • Properties (constants): accessed without parentheses, e.g., Math.PI, Math.E, Math.SQRT2.
  • Methods (functions): always called with parentheses, e.g., Math.round(4.6), Math.sqrt(16).
  • No instantiation: Math is not a constructor. This is invalid: new Math().

A common pattern for generating a random integer between two values is:

Math.floor(Math.random() * (max - min + 1)) + min

? Code Examples

? Math Properties and Basic Methods

? View Code Example
// Math properties (constants)
console.log(Math.PI);      // 3.141592653589793
console.log(Math.E);       // 2.718281828459045
console.log(Math.SQRT2);   // 1.4142135623730951
console.log(Math.LN2);     // 0.6931471805599453

// Math methods
console.log(Math.round(4.6));   // 5  (nearest integer)
console.log(Math.ceil(4.1));    // 5  (round up)
console.log(Math.floor(4.9));   // 4  (round down)
console.log(Math.trunc(4.9));   // 4  (remove decimals)
console.log(Math.pow(2, 3));    // 8  (2 to the power of 3)
console.log(Math.sqrt(16));     // 4  (square root)
console.log(Math.abs(-7));      // 7  (absolute value)

console.log(Math.max(1, 5, 9)); // 9  (largest value)
console.log(Math.min(1, 5, 9)); // 1  (smallest value)

? Math.random() Basics

? View Code Example
// A random number between 0 (inclusive) and 1 (exclusive)
console.log(Math.random()); // e.g., 0.47283478

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

// Random integer between 1 and 100
const random1to100 = Math.floor(Math.random() * 100) + 1;
console.log(random1to100);

// Helper: random integer between min and max (inclusive)
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

console.log(getRandomInt(1, 6));    // e.g., 4 (like a dice roll)
console.log(getRandomInt(50, 100)); // e.g., 73

? More Random Examples

? View Code Example
// Random item from an array
const colors = ["Red", "Green", "Blue"];
const randomIndex = Math.floor(Math.random() * colors.length);
const randomColor = colors[randomIndex];
console.log(randomColor);

// Dice roll (1 to 6)
const dice = Math.floor(Math.random() * 6) + 1;
console.log("Rolled:", dice);

? Live Output and Explanation

? What the code does

  • Math.PI, Math.E, Math.SQRT2, and Math.LN2 give you commonly used mathematical constants.
  • Math.round(), Math.floor(), Math.ceil(), and Math.trunc() help you control how decimal numbers are converted to integers.
  • Math.max() and Math.min() find the largest and smallest values from a list of numbers.
  • Math.random() on its own returns a decimal between 0 and 1. Multiplying it and using Math.floor() lets you create integer ranges.
  • The helper getRandomInt(min, max) function wraps this pattern into a reusable piece of code for any integer range.
  • In the array example, a random index based on colors.length is used to pick a random color from the list.

? Interactive Example

? Roll a Virtual Dice

Click the button to roll a dice (values from 1 to 6) using Math.random() and Math.floor().

Current roll: -

? Tips & Best Practices

  • Math is not a constructor; never use new Math().
  • Use Math.random() together with Math.floor() (or Math.trunc()) to generate random integers.
  • Use helper functions like getRandomInt(min, max) instead of repeating the same random-range logic everywhere.
  • Use Math.abs() to remove negative signs when you only care about magnitudes.
  • Always keep in mind that Math.random() is pseudo-random and not suitable for cryptographic purposes.

? Try It Yourself

  • Generate a random number between 50 and 100 using getRandomInt() or by writing the expression yourself.
  • Round 3.14159 using Math.round(), Math.floor(), and Math.ceil() and compare the results.
  • Use Math.max() and Math.min() to find the largest and smallest values from an array of numbers.
  • Write a function that simulates rolling two dice and returns their total.
  • Create a simple “random color picker” that changes the background color of the page using the colors array example.