One number type
Nearly every program computes something, whether that is prices at checkout, scores in a game, or the minutes left on a timer, so arithmetic is the first tool to move over from Python. The good news is that it transfers almost unchanged, with one operator missing and one type merged.
Python has separate int and float types. JavaScript has just one, called number. Both 5 and 5.5 are numbers, and division always gives you the exact decimal result.
The operators match Python almost exactly:
| Operation | Python | JavaScript |
|---|---|---|
| add, subtract, multiply | + - * | + - * |
| division | / | / |
| remainder (modulo) | % | % |
| power | ** | ** |
| floor division | // | Math.floor(a / b) |
The one to remember is that JavaScript has no // operator. To divide and drop the decimal part, divide normally and round down with Math.floor(...).
The operators side by side
Five expressions that cover every difference from Python. Read each one against the output below.
The third line is the remainder of 7 divided by 2, and the last uses Math.floor to reproduce Python's 7 // 2. Note the second line: 7 / 2 gives 3.5, not 3, because JavaScript has only the one number type and never silently truncates.
console.log(7 + 3); console.log(7 / 2); console.log(7 % 2); console.log(2 ** 10); console.log(Math.floor(7 / 2));
Output
10 3.5 1 1024 3
The Math toolbox
Math is a built-in object full of helpers, like Python's math module but always available with no import:
Math.floor(3.9)→ 3 (round down)Math.round(3.5)→ 4 (round to nearest)Math.abs(-8)→ 8 (absolute value)Math.max(2, 9, 4)→ 9 andMath.min(2, 9, 4)→ 2Math.sqrt(81)→ 9 (square root, √81)
You call them with a dot, Math.floor(x), because they live on the Math object. Objects get a full treatment in unit 5.
Splitting seconds into minutes and seconds
Turning a duration into whole minutes plus leftover seconds is the classic use of division and remainder together. With 400 seconds, Math.floor(400 / 60) gives the whole minutes and 400 % 60 gives what is left over.
console.log(Math.floor(400 / 60)); console.log(400 % 60);
Output
6 40
400 / 60 is 6.666..., and Math.floor cuts that down to 6. The remainder is 40, because six whole minutes account for 360 seconds and 400 − 360 = 40.
Division and remainder always travel together for this pattern.
Math.floorcounts the whole groups that fit, and%reports how much did not fit into any of them.
Computing a tip from variables
Here a bill of 50 meets a tip rate of 0.2, which is 20%. Both the tip and the total are computed rather than typed in, which is the point of the example.
const bill = 50; const tipRate = 0.2; const tip = bill * tipRate; console.log(tip); console.log(bill + tip);
Output
10 60
Storing the tip in its own variable means the total can reuse it as bill + tip, so the rate appears exactly once in the program. Change tipRate and both printed lines follow automatically.
Why the output has no decimal point
The tip is bill * tipRate, which is 50 × 0.2 = 10, and the total is 60. Those print as 10 and 60, not 10.0 and 60.0, because JavaScript has one number type and drops a trailing .0 when it turns a number into text. A value being whole is not the same as it having an integer type here, since no integer type exists.
The JavaScript spelling of 7 // 2
Python's 7 // 2 gives 3. The JavaScript expression for the same result is Math.floor(7 / 2).
There is no // operator here, and it is not merely missing: // starts a comment in JavaScript, so writing 7 // 2 leaves you with the expression 7 and a comment. Plain 7 / 2 gives 3.5, since one number type means division never truncates, and Math.floor rounds that down to 3. Do not reach for 7 % 2 by mistake, that is the remainder, 1.