JavaScript Math

</>Coding Tamilan.← JavaScript course

JavaScript · Lesson 17 / 32 · English

JavaScript Math

Understand the idea. Change the code. Build confidence with one focused experiment.

Detailed notesRunnable JavaScriptPractice + answer

Read this lesson in தமிழ் ↗.

01 / Understand

Start with the idea

Math provides static numeric utilities. Pick the rounding rule that matches the requirement: floor, ceil, round, and trunc are not interchangeable.

Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.

02 / Explore

Step-by-step lesson notes

The Math Object

The Math object provides mathematical constants and functions. It is not a constructor — all properties and methods are static.

Math Constants

JavaScript reference fragmentCODE
Math.PI;       // 3.141592653589793
Math.E;        // 2.718281828459045 (Euler's number)
Math.SQRT2;    // 1.4142135623730951
Math.LN2;      // 0.6931471805599453
Math.LOG2E;    // 1.4426950408889634

Common Math Methods

JavaScript reference fragmentCODE
// Rounding
Math.round(4.5);   // 5 (rounds to nearest)
Math.ceil(4.1);    // 5 (always rounds up)
Math.floor(4.9);   // 4 (always rounds down)
Math.trunc(4.9);   // 4 (removes decimal part)

// Absolute value
Math.abs(-5);      // 5

// Power & Root
Math.pow(2, 8);    // 256
Math.sqrt(16);     // 4
Math.cbrt(27);     // 3 (cube root)

// Min / Max
Math.min(1, 2, 3);   // 1
Math.max(1, 2, 3);   // 3
Math.min(...[5, 2, 8, 1]); // 1 (use spread for arrays)

// Logarithm
Math.log(Math.E);  // 1
Math.log2(8);      // 3
Math.log10(1000);  // 3

// Trigonometry (angles in radians)
Math.sin(Math.PI / 2);  // 1
Math.cos(0);             // 1

Random Numbers

JavaScript reference fragmentCODE
// Math.random() → [0, 1) exclusive of 1
Math.random(); // e.g. 0.7823...

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

console.log(randomInt(1, 6));   // dice roll: 1-6
console.log(randomInt(0, 100)); // 0-100

// Random element from array
const items = ["rock", "paper", "scissors"];
const pick = items[randomInt(0, items.length - 1)];
03 / Read & predict

A complete example

Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.

index.js · complete exampleCODE
const score = 7.6;
console.log(Math.floor(score));
console.log(Math.ceil(score));
console.log(Math.round(score));
console.log(Math.max(12, 28, 19));
Predict first. Reveal the expected result.
7
8
8
28
04 / Experiment

Make one change. See what happens.

Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.

Open editor ↗

Wait for the editor to load, then load this example and press Run.

Powered by the free OneCompiler editor. Code you run is sent to that external service. Keep personal data and secret keys out of your examples.

05 / Your turn

Put the idea to work

Practice challenge

Change score to -7.6 and compare floor with trunc.

Reveal the answer and reasoning

Math.floor(-7.6) is -8; Math.trunc(-7.6) is -7.

One mistake to avoid: Math.random is not suitable for passwords or security tokens. Use a cryptographically secure API for security requirements.

Your learning checkpoint

  • Explain the example in your own words.
  • Complete the challenge before revealing its answer.
  • Describe the common mistake and how to avoid it.
Coding Tamilan · Learn by doing.