Skip to content
Codewilds

Lesson 3 of 34 · Fundamentals

JavaScript Numbers and Math Operators

easy10 XP3 min readFree lesson

Games are full of math: damage, gold, experience points, cooldown timers. JavaScript makes number crunching easy, as long as you know a few rules and a couple of famous gotchas.

This lesson also introduces a small but important change: from now on you'll write your code inside functions, so the tests can try your code with many different inputs.

One number type

Unlike many languages, JavaScript has a single number type for both whole numbers and decimals:

const gold = 150;
const price = 19.99;
console.log(typeof gold, typeof price); // number number

Arithmetic operators

console.log(7 + 2);  // 9   addition
console.log(7 - 2);  // 5   subtraction
console.log(7 * 2);  // 14  multiplication
console.log(7 / 2);  // 3.5 division (always "real" division)
console.log(7 % 2);  // 1   remainder after division
console.log(7 ** 2); // 49  exponent (7 to the power of 2)

Operators follow the usual math precedence: *, / and % run before + and -. Use parentheses to make the order explicit:

console.log(2 + 3 * 4);   // 14
console.log((2 + 3) * 4); // 20

The remainder operator % is surprisingly useful. n % 2 === 0 tells you whether n is even, and minutes % 60 gives the minutes left over after counting full hours.

The Math object

JavaScript has a built-in Math object full of helpers:

console.log(Math.round(4.5));  // 5  nearest whole number
console.log(Math.floor(4.9));  // 4  round down
console.log(Math.ceil(4.1));   // 5  round up
console.log(Math.abs(-12));    // 12 distance from zero
console.log(Math.max(3, 9, 4)); // 9
console.log(Math.min(3, 9, 4)); // 3
console.log(Math.sqrt(81));    // 9
console.log(Math.PI);          // 3.141592653589793

There's no separate "integer division" operator. To get whole results, divide and then round: Math.floor(17 / 5) is 3.

Floating-point surprises

Decimals are stored in binary, so some values can't be represented exactly:

console.log(0.1 + 0.2); // 0.30000000000000004

This isn't a JavaScript bug. Most programming languages behave the same way. When you need a tidy result, round it: Math.round(x * 100) / 100 rounds to two decimals.

Two more special values exist: dividing a positive number by zero gives Infinity, and math that makes no sense gives NaN ("Not a Number"), for example "abc" * 2.

Strings that look like numbers

The + operator also joins strings, so mixing types can surprise you:

console.log("5" + 3);         // 53 (string joining!)
console.log("5" * 3);         // 15 (* converts to numbers)
console.log(Number("5") + 3); // 8

When a value might be a string, convert it with Number(...) before doing math.

Writing code inside a function

Here's the shape of the code you'll be completing from now on:

function double(n) {
  return n * 2;
}

console.log(double(4));  // 8
console.log(double(10)); // 20

n is the input, and return sends the answer back to whoever called the function. Chapter 2 covers functions in depth. For now, just replace the placeholder return with the right calculation.

Common mistakes

  • Expecting whole numbers from /. 7 / 2 is 3.5, not 3. Round with Math.floor when you need whole units.
  • Adding strings. "5" + 3 is "53". Convert first with Number().
  • Comparing decimals exactly. 0.1 + 0.2 === 0.3 is false. Round before comparing.

Assignment

Complete three functions:

  1. toFahrenheit(celsius) returns the temperature converted to Fahrenheit using F = C × 9 / 5 + 32. Example: toFahrenheit(100) returns 212.
  2. coinsEach(gold, players) returns how many whole coins each player gets when gold is shared evenly. Example: coinsEach(100, 3) returns 33.
  3. coinsLeft(gold, players) returns how many coins are left over after sharing. Example: coinsLeft(100, 3) returns 1.

You can assume gold is a whole number of at least 0 and players is at least 1.

Numbers and math operators in other languages

Need a quick reminder? The JavaScript cheat sheet has every topic of Learn JavaScript on one page.

Lesson last updated on .

Write code ↓
JavaScript+10 XP

Press Run to execute your code, or Submit to check it against the tests.