Emmanuel Akinfulubi

JavaScriptMath

Volume and surface area of a cube and cuboid in JavaScript

Beginner · 4 min read

How much does a box hold? How much cardboard does it take to make it? Will a long umbrella fit inside it, corner to corner? These are the three measurements of a box: its volume, its surface area and its space diagonal. A box with all sides equal is a cube. A box with different sides is a cuboid (also called a rectangular prism).

Shipping calculators, storage apps and 3D games all use these. In this tutorial you will write a function for each, use them on a cube of side 3 and a box 2 by 3 by 6, and then see what doubling the side does to each figure. You need to know arrow functions and ** for powers.

Volume and surface area

For a box of width w, height h and depth d:

  • Volume = w × h × d, the number of unit cubes that fit inside. For a cube of side s, that is s³.
  • Surface area is the total area of all six faces. They come in three matching pairs: front and back (w × h), top and bottom (w × d), left and right (h × d). So the total is 2 × (wh + hd + wd). A cube has six equal square faces, so it is 6 × s².

The space diagonal

The space diagonal goes from one bottom corner to the opposite top corner, right through the middle of the box. Pythagoras works in three dimensions too: diagonal² = w² + h² + d².

Math.hypot takes as many numbers as you like and returns the square root of the sum of their squares:

JavaScript

console.log(Math.hypot(3, 4));    // 5
console.log(Math.hypot(1, 2, 2)); // 3

Here is the exercise. Try it yourself first if you like, then read on and we will build it together.

76 A cube and a cuboid: volume, surface and the space diagonal

easy

For a cube of side 3 and a cuboid 2 by 3 by 6, give the volume, the total surface area and the corner-to-corner diagonal. Then double the cube's side and say what happens to each figure.

Stuck? Three hints, smallest first
  1. 1. A cuboid has three pairs of faces, so the surface is 2 * (wh + hd + wd).
  2. 2. The space diagonal is Pythagoras in three dimensions, which Math.hypot takes directly.
  3. 3. Volume scales with the cube of a length, surface with the square.

Step 1: the cube

Step 1

const cubeVolume = (s) => s ** 3;
const cubeSurface = (s) => 6 * s ** 2;
const spaceDiagonal = (w, h, d) => Math.hypot(w, h, d);
const s = 3;
console.log(cubeVolume(s), cubeSurface(s)); // 27 54
console.log(spaceDiagonal(s, s, s));        // 5.196152422706632

The cube holds 27 unit cubes and has 54 square units of surface. A cube's diagonal is always its side times √3, because s² + s² + s² is 3s²:

JavaScript

console.log(s * Math.sqrt(3)); // 5.196152422706632

The two agree to the last digit here. That isn't guaranteed for every size: Math.hypot takes extra care to avoid overflow with huge numbers, so it can differ in the last digit from a hand-written square root.

Step 2: the cuboid

Step 2

const cuboidVolume = (w, h, d) => w * h * d;
const cuboidSurface = (w, h, d) => 2 * (w * h + h * d + w * d);
console.log(cuboidVolume(2, 3, 6), cuboidSurface(2, 3, 6)); // 36 72
console.log(spaceDiagonal(2, 3, 6));                        // 7

The faces are 2 × 3 = 6, 3 × 6 = 18 and 2 × 6 = 12. They add up to 36, and doubled that is 72. The diagonal is exactly 7, because 4 + 9 + 36 = 49, and √49 = 7.

A cube is just a cuboid with three equal sides, so the cuboid functions give the same answers for it:

JavaScript

console.log(cuboidVolume(3, 3, 3), cuboidSurface(3, 3, 3)); // 27 54

Step 3: double the side

What happens when the cube's side goes from 3 to 6?

Step 3

console.log(cubeVolume(6), cubeSurface(6)); // 216 216
console.log(cubeVolume(6) / cubeVolume(3), cubeSurface(6) / cubeSurface(3)); // 8 4

The surface area goes up 4 times, because it uses two lengths (2 × 2 = 4). The volume goes up 8 times, because it uses three (2 × 2 × 2 = 8). Many people guess "twice as much" for both. A box with double the sides holds eight times as much.

Step 4: surface compared with volume

Because volume grows faster than surface, the amount of surface for each unit of volume goes down as the cube gets bigger:

Step 4

console.log(cubeSurface(1) / cubeVolume(1)); // 6
console.log(cubeSurface(s) / cubeVolume(s), 6 / s); // 2 2
console.log(cubeSurface(6) / cubeVolume(6)); // 1

For a cube, surface divided by volume is 6 / s. A small cube has a lot of surface for its size, and a big one has little.

This idea explains everyday things. Crushed ice melts faster than a block of ice, because more of it touches the warm air. A small animal loses heat faster than a large one for the same reason.

The full solution

The answer panel has the cube and cuboid volume and surface functions and spaceDiagonal, and prints the cube of side 3, its diagonal as side times √3, the 2 by 3 by 6 box, the doubled cube with its ratios, surface over volume, and the unit cube. Press Run to see the output.

The answer

const cubeVolume = (s) => s ** 3;
const cubeSurface = (s) => 6 * s ** 2;

const cuboidVolume = (w, h, d) => w * h * d;
const cuboidSurface = (w, h, d) => 2 * (w * h + h * d + w * d);
const spaceDiagonal = (w, h, d) => Math.hypot(w, h, d);

const s = 3;

console.log('cube 3           :', cubeVolume(s), cubeSurface(s), spaceDiagonal(s, s, s));
console.log('that diagonal is :', s * Math.sqrt(3));
console.log('cuboid 2 x 3 x 6 :', cuboidVolume(2, 3, 6), cuboidSurface(2, 3, 6), spaceDiagonal(2, 3, 6));
console.log('side doubled     :', cubeVolume(6), cubeSurface(6));
console.log('ratios           :', cubeVolume(6) / cubeVolume(3), cubeSurface(6) / cubeSurface(3));
console.log('surface / volume :', cubeSurface(s) / cubeVolume(s), 'which is 6 / s');
console.log('unit cube        :', cubeVolume(1), cubeSurface(1));

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.

8 checks

test('a cube of side 3 has volume 27 and surface 54', [cubeVolume(s), cubeSurface(s)], [27, 54]);
test('its space diagonal is s times root 3, to the last bit', [spaceDiagonal(s, s, s), spaceDiagonal(s, s, s) === s * Math.sqrt(3)], [5.196152422706632, true]);
test('the 2 by 3 by 6 cuboid holds 36 and has 72 of surface', [cuboidVolume(2, 3, 6), cuboidSurface(2, 3, 6)], [36, 72]);
test('and its diagonal is exactly 7, since 4 + 9 + 36 is 49', spaceDiagonal(2, 3, 6), 7);
test('doubling the side multiplies the volume by 8 and the surface by 4', [cubeVolume(6) / cubeVolume(3), cubeSurface(6) / cubeSurface(3)], [8, 4]);
test('so surface over volume is 6 / s, and it falls as the cube grows', [cubeSurface(s) / cubeVolume(s), 6 / s, cubeSurface(6) / cubeVolume(6)], [2, 2, 1]);
test('the ratio is 6 for a unit cube and 1 at side 6', [cubeSurface(1) / cubeVolume(1), cubeSurface(6) / cubeVolume(6)], [6, 1]);
test('a cube is the cuboid case where all three sides agree', [cuboidVolume(3, 3, 3), cuboidSurface(3, 3, 3)], [27, 54]);

The scaling law is the content here. Double a length and the surface goes up four times while the volume goes up eight, so the surface-to-volume ratio falls — 6 / s for a cube. That is why a small animal loses heat faster than a large one, why crushed ice melts faster than a block, and why cooling a data centre gets harder as the rack gets denser rather than easier.

The space diagonal is just Math.hypot with three arguments, and it is the number that decides whether a parcel fits in a box diagonally. Both of these examples come out exact: 2, 3, 6 is a Pythagorean quadruple, so the diagonal is precisely 7, and 3 * Math.sqrt(3) matches Math.hypot(3, 3, 3) bit for bit. Neither is guaranteed in general — Math.hypot scales its inputs before squaring to avoid overflow, so it can return a different last bit from a hand-written Math.sqrt of a sum.

Common mistakes

  • Counting only three faces. A box has six. Without the 2 × the surface area is half what it should be.
  • Thinking double the side means double the volume. It means eight times the volume and four times the surface.
  • Using the face diagonal instead of the space diagonal. Math.hypot(w, h) is across one face. Corner to corner through the box needs all three sides.

Try it yourself

  1. A shipping box is 40 cm by 30 cm by 20 cm. Find its volume in liters (1 liter is 1000 cm³).
  2. Will a 70 cm umbrella fit inside that box, corner to corner?
  3. Find the side of a cube that holds 1000 unit cubes, using Math.cbrt, and give its surface area.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.