Area, perimeter and diagonal of a rectangle in JavaScript
Beginner · 4 min read
How much paint covers a wall? How much fencing goes around a garden? Will a 2-meter pole fit across a doorway? These are three questions about one rectangle: its area (the space inside), its perimeter (the distance around the edge) and its diagonal (the distance from one corner to the opposite one).
In this tutorial you will write a small function for each, use them on a rectangle 7.5 by 4, and then learn what happens when you double the sides or change the units. Most mistakes in this kind of code are about units, not arithmetic. You need to know arrow functions and ** for powers.
The three formulas
For a rectangle of width w and height h:
- Area =
w × h. A 7.5 by 4 rectangle covers 30 square units. - Perimeter =
2 × (w + h): two widths plus two heights, so 2 × 11.5 = 23. - Diagonal: the diagonal and two sides make a right-angled triangle, so Pythagoras applies: diagonal² = w² + h².
Math.hypot
Math.hypot(a, b) works out √(a² + b²) for you. "Hypot" is short for hypotenuse, the long side of a right-angled triangle.
console.log(Math.hypot(3, 4)); // 5
It is shorter to write than Math.sqrt(a * a + b * b), and it also copes with very large numbers whose squares would be too big to store.
Here is the exercise. Try it yourself first if you like, then read on and we will build it together.
72 A rectangle: area, perimeter and the diagonal
easyFor a rectangle 7.5 by 4, give the area, the perimeter and the diagonal. Then double both sides and say what happens to each, compare it with the square of the same perimeter, and convert the area from square centimetres to square metres.
Stuck? Three hints, smallest first
- 1. The diagonal is Pythagoras on the two sides, so
Math.hypotdoes it. - 2. Doubling both sides does not double the area.
- 3. A length divides by 100 to go from centimetres to metres, so an area divides by 100 twice.
Step 1: the three functions
const area = (w, h) => w * h;
const perimeter = (w, h) => 2 * (w + h);
const diagonal = (w, h) => Math.hypot(w, h);
const w = 7.5;
const h = 4;
console.log(area(w, h), perimeter(w, h)); // 30 23
console.log(diagonal(w, h)); // 8.5
Each formula gets its own one-line function, so the names say what each number means.
Step 2: check the diagonal
8.5 is a neat answer. You can check it: the squares of the sides should add up to the square of the diagonal.
console.log(w ** 2 + h ** 2, 8.5 ** 2); // 72.25 72.25
56.25 + 16 = 72.25, and 8.5² is 72.25 too. These numbers are exact in JavaScript: 7.5, 4 and 8.5 all have short binary forms, so nothing needs rounding. That won't be true for circles in the next exercise.
Step 3: double both sides
What happens to each figure if the rectangle becomes 15 by 8?
console.log(area(2 * w, 2 * h), perimeter(2 * w, 2 * h)); // 120 46
The perimeter doubles, from 23 to 46. The area goes from 30 to 120: four times bigger, not two. Each side is twice as long, and area multiplies the two sides, so it grows by 2 × 2 = 4. Many people guess "double", and their paint or flooring estimate comes out half of what is needed.
Step 4: the same perimeter as a square
A square with the same 23 units of fencing has sides of 23 ÷ 4 = 5.75:
const side = perimeter(w, h) / 4; // the square with the same perimeter
console.log(side, area(side, side)); // 5.75 33.0625
The square encloses 33.0625, more than our rectangle's 30. Among all rectangles with the same perimeter, the square always has the largest area. So the perimeter alone doesn't tell you the area:
console.log(area(10, 1), perimeter(10, 1)); // 10 22
console.log(area(4, 7), perimeter(4, 7)); // 28 22
Both rectangles need 22 units of fencing, but one encloses almost three times as much ground.
Step 5: square centimeters to square meters
Say the rectangle was measured in centimeters, so the area is 30 square centimeters. A length goes from centimeters to meters by dividing by 100. An area has two lengths multiplied together, so it divides by 100 twice, which is 10,000:
console.log(area(w, h) / 10000); // 0.003
30 cm² is 0.003 m². Dividing by just 100 would give 0.3, a hundred times too big. This is the mistake behind many wrong tile and carpet quotes.
Step 6: a side of zero
console.log(area(w, 0), perimeter(w, 0)); // 0 15
A "rectangle" with no height is just a line. It covers no area, but walking around it still means going 7.5 there and 7.5 back. The functions return honest numbers for it. If your app should refuse such shapes, check for them yourself.
The full solution
The answer panel has area, perimeter and diagonal, and prints the 7.5 by 4 results, the diagonal check, the doubled rectangle, the square with the same perimeter, the area in square meters, and the zero-height case. Press Run to see the output.
const area = (w, h) => w * h;
const perimeter = (w, h) => 2 * (w + h);
const diagonal = (w, h) => Math.hypot(w, h);
const w = 7.5;
const h = 4;
const side = perimeter(w, h) / 4; // the square with the same perimeter
console.log('area, perimeter :', area(w, h), perimeter(w, h));
console.log('diagonal :', diagonal(w, h), 'and 7.5 ** 2 + 4 ** 2 is', w ** 2 + h ** 2);
console.log('both sides x2 :', area(2 * w, 2 * h), perimeter(2 * w, 2 * h));
console.log('same perimeter :', side, 'square area', area(side, side));
console.log('cm2 to m2 :', area(w, h) / 10000);
console.log('degenerate :', area(w, 0), perimeter(w, 0));
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('7.5 by 4 has an area of 30 and a perimeter of 23', [area(w, h), perimeter(w, h)], [30, 23]);
test('the diagonal is exactly 8.5, because 56.25 + 16 is 72.25', [diagonal(w, h), w ** 2 + h ** 2, 8.5 ** 2], [8.5, 72.25, 72.25]);
test('doubling both sides quadruples the area and doubles the perimeter', [area(2 * w, 2 * h), perimeter(2 * w, 2 * h)], [120, 46]);
test('a square of the same perimeter has the larger area', [side, area(side, side), area(side, side) > area(w, h)], [5.75, 33.0625, true]);
test('30 square centimetres is 0.003 square metres, dividing by 100 twice', area(w, h) / 10000, 0.003);
test('a zero side leaves no area but a real perimeter', [area(w, 0), perimeter(w, 0)], [0, 15]);
test('area and perimeter do not determine each other', [area(10, 1), perimeter(10, 1), area(4, 7), perimeter(4, 7)], [10, 22, 28, 22]);
test('and the diagonal never exceeds half the perimeter', diagonal(w, h) < perimeter(w, h) / 2, true);
The arithmetic is trivial and the units are where the mistakes live. Area scales with the square of a length change: doubling both sides multiplies the area by 4, and converting centimetres to metres divides the area by 10,000 rather than 100. The same applies to a tiling or pricing calculation done per square metre from measurements taken in centimetres.
Two facts worth carrying. Area and perimeter are independent — 10 by 1 and 4 by 7 share a perimeter of 22 and have areas of 10 and 28 — and among all rectangles with a given perimeter the square has the largest area, which is why 5.75 by 5.75 beats 7.5 by 4. And these numbers are exact doubles: 7.5, 4 and 8.5 all have short binary expansions, so nothing here needs rounding. That will not be true in the next exercise.
Common mistakes
- Thinking doubling the sides doubles the area. It multiplies the area by 4.
- Converting an area like a length. cm² to m² divides by 10,000, not 100.
- Writing
2 * w + hfor the perimeter. Without the brackets, only the width is doubled. Write2 * (w + h).
Try it yourself
- A room is 4.2 m by 3.5 m. How many square meters of flooring does it need? Round to two decimal places.
- Will a 2.5 m pole fit diagonally through a doorway 0.9 m wide and 2.1 m tall? Use
diagonal. - Write
areaInSquareMeters(wCm, hCm)that takes centimeters and returns square meters.