Why This Matters
Every calculation you will ever perform rests on a simple idea: numbers live on a line. The number line stretches infinitely in both directions, with zero at the center. To the right sit the positive numbers, to the left the negatives. This mental model is the scaffold on which all of arithmetic is built.
Integers are the whole numbers on this line: ... -3, -2, -1, 0, 1, 2, 3 ... They have no fractions, no decimals, just clean steps. Understanding integers means understanding direction and distance. Is a number to the left or right of zero? How far apart are two numbers? The concept of absolute value captures that pure distance, stripping away direction entirely.
In programming, integers are the most fundamental numeric type. Array indices are integers. Loop counters are integers. Memory addresses are integers. Before you can add, subtract, multiply, or divide, you need to understand the terrain those operations live on.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
The number line: integers stretch infinitely in both directions, with zero as the origin. Absolute value measures distance from zero.
Code Example
// Integers in JavaScript
const positive = 7;
const negative = -3;
const zero = 0;
console.log(positive, negative, zero); // 7 -3 0
// Absolute value
console.log(Math.abs(-5)); // 5
console.log(Math.abs(5)); // 5
console.log(Math.abs(0)); // 0
// Distance between two integers on the number line
function distance(a, b) {
return Math.abs(a - b);
}
console.log(distance(3, -4)); // 7
console.log(distance(-2, -8)); // 6
console.log(distance(5, 5)); // 0
// Checking if a number is an integer
console.log(Number.isInteger(42)); // true
console.log(Number.isInteger(3.14)); // false
console.log(Number.isInteger(-7)); // true
// Integer properties
console.log(Math.sign(-10)); // -1 (negative)
console.log(Math.sign(0)); // 0 (zero)
console.log(Math.sign(10)); // 1 (positive)Interactive Experiment
Try these exercises:
- Pick any negative integer and compute its absolute value. Then pick its positive mirror. Are the absolute values the same? Why?
- Compute the distance between -100 and 100 using
Math.abs(a - b). Is it the same asMath.abs(b - a)? Why does order not matter? - What is
Math.abs(0)? Is zero positive, negative, or neither? - List all integers between -5 and 5 inclusive. How many are there? (Hint: it is not 10.)
- What happens when you compute
Math.abs(-2147483648)in JavaScript? Research why this edge case exists.
Quick Quiz
Coding Challenge
Write a function called `numberLineDistance` that takes two integers and returns the distance between them on the number line. The distance is always a non-negative integer. Do NOT use any built-in absolute value function (Math.abs or abs). Instead, implement the logic yourself using a conditional.
Real-World Usage
The number line and integers are everywhere in computing:
- Array indexing: Arrays use integer indices starting from 0. The index tells you exactly how many positions from the start an element lives.
- Temperature scales: Celsius temperatures go negative. Weather apps calculate temperature differences using the same integer distance concept from this lesson.
- Financial systems: Bank balances can be positive (credit) or negative (debt). The absolute value tells you the magnitude regardless of direction.
- Coordinate systems: Computer graphics use integer coordinates (pixels). Game engines use signed integers for positions that can be left/right or up/down of an origin point.
- Timezone offsets: UTC offsets range from -12 to +14. Computing time differences between zones is integer distance on a number line.