LESSON 8

Numbers

Learning objective: Work with numbers and basic math operations.

Understand

JavaScript has a single number type for integers and decimals.

You can do arithmetic directly, and the Math object offers helpers like Math.round, Math.random, and Math.max. Watch out for floating-point quirks like 0.1 + 0.2.

Analogy: The Math object is like a scientific calculator built into the language.

See It in Action

Rounding and random:

Math.round(4.6);   // 5
Math.max(3, 9, 1); // 9
Math.random();     // 0..1
How it works: Math.round rounds to the nearest integer, Math.max finds the largest, and Math.random returns a decimal between 0 and 1.

Try It Yourself

  1. Round a decimal with Math.round.
  2. Find the largest of three numbers.
  3. Generate a random number.

Quick Quiz

Which gives a random number between 0 and 1?

Challenge

Generate a random whole number between 1 and 10.

Success condition: Your code prints a random integer within the range each run.