Numbers Lesson

Core idea: Numbers are the data type JavaScript uses for counting, measuring, and calculating. A number can be a whole value like 10 or a decimal like 3.5. In a program, numbers are used for scores, prices, positions, speeds, sizes, timers, and any value that needs math.

The important thing about numbers is that they can be changed by arithmetic. A variable can start with one value, then increase, decrease, multiply, divide, or be compared against another number. This is what makes numbers useful for real program behavior instead of just displaying information.

Example in Use

Code Runner Challenge

Numbers Lesson

View IPYNB Source
%%js

// CODE_RUNNER: Numbers Lesson

let playerX = 40;
let speed = 6;
let coins = 0;

playerX = playerX + speed;
coins = coins + 1;

console.log("Player x position: " + playerX);
console.log("Coins collected: " + coins);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

When a game moves a player across the screen, the x position is usually a number. Each update adds speed to that position, so the object appears to move. The same idea works for gravity, health, points, and countdowns.