Zum Inhalt springen
KodeTrail

Camp 2 · Schritt 5 von 18

Numbers and math

One number type, all the operators, and Math — JavaScript's built-in toolbox.

12 Min.+50 XPAuf Englisch angezeigt — Übersetzung ist unterwegs

Where Python distinguishes ints from floats, JavaScript keeps it simple: one number type covers 42, -3.5, and 0.0001 alike.

The operators

JavaScript

% (remainder) is the unfamiliar hero: n % 2 tells you even or odd, minutes % 60 gives leftover minutes, index % length wraps around lists.

Shortcut operators

Changing a variable based on its current value is so common there's shorthand:

JavaScript

The Math toolbox

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

JavaScript

Math.random() gives a random number between 0 and 1 — run it twice and watch it change. It's the seed of every dice roll and shuffle you'll write.

Checkpoint

What does 10 % 4 evaluate to?

Checkpoint

After let x = 7; x += 3; x++; what is x?

What's next

Strings — template literals will do for JavaScript what f-strings did for Python.