DEV Community

Farbod F.
Farbod F.

Posted on

The BMI formula is almost 200 years old: implementing it (and its caveats) in JS

Body mass index is one line of arithmetic:

function bmi(weightKg, heightM) {
  return weightKg / (heightM * heightM);
}
Enter fullscreen mode Exit fullscreen mode

Adolphe Quetelet published it in 1832 — 194 years ago — as a way to describe populations, not people. It was never designed to judge an individual's health. And yet it is still the first number most health apps, insurance forms, and gym intake sheets ask for. So if you're going to implement it, implement it honestly: get the math right, and show the user where the number stops meaning anything.

The worked example

Take someone 1.78 m tall weighing 80 kg:

BMI = 80 / (1.78 × 1.78) = 80 / 3.1684 = 25.2
Enter fullscreen mode Exit fullscreen mode

The WHO bands are: under 18.5 underweight, 18.5–24.9 healthy, 25.0–29.9 overweight, 30 and above obese. So 25.2 lands just inside "overweight" — by 0.2 of a point.

Here's the part most implementations never show the user. At 1.78 m, the healthy/overweight boundary sits at:

25.0 × 3.1684 = 79.2 kg
Enter fullscreen mode Exit fullscreen mode

while the top of "healthy" is:

24.9 × 3.1684 = 78.9 kg
Enter fullscreen mode Exit fullscreen mode

The gap between "healthy" and "overweight" at this height is 317 grams — about one large glass of water. A single meal, a full bladder, or weighing yourself in the evening instead of the morning can move you across a category boundary that sounds like a medical verdict. Any UI that prints "overweight" in red without showing the distance to the boundary is overstating its own precision.

A second example shows the opposite failure. A 1.80 m rugby player at 100 kg:

BMI = 100 / (1.80 × 1.80) = 100 / 3.24 = 30.9
Enter fullscreen mode Exit fullscreen mode

That's "obese" by the table — for someone who may carry very little body fat. BMI cannot see muscle. It also cannot see age, sex, or where fat is carried, which is why clinicians pair it with waist circumference or waist-to-height ratio rather than using it alone.

The implementation details that matter

Three things to get right in code:

1. Validate the inputs. Heights in centimetres are the classic bug — a user types 178 and your function returns 0.0025. Guard the plausible range (say 0.5–2.5 m, 20–300 kg) and reject the rest with a clear message.

2. Round for display, never for logic. 25.249 should display as 25.2, but classify on the unrounded value — rounding 24.96 up to 25.0 and then calling it overweight is a classification error, not a display choice.

3. Show the bands, not just the verdict. The useful output is the number plus where it sits: "25.2 — 0.3 above the healthy range, 4.8 below obese." That framing is honest about how close to a boundary the person is, and it survives the 317-gram problem above.

const BANDS = [
  { max: 18.5, label: "Underweight" },
  { max: 25.0, label: "Healthy weight" },
  { max: 30.0, label: "Overweight" },
  { max: Infinity, label: "Obese" },
];

function classify(value) {
  return BANDS.find((b) => value < b.max).label;
}
Enter fullscreen mode Exit fullscreen mode

Honest caveats

  • BMI was built for populations. On an individual it is a screening signal, not a diagnosis — it says "worth a closer look", never "you are unhealthy".
  • It misclassifies in both directions: muscular people score too high, older adults with muscle loss can score "healthy" while carrying risky visceral fat.
  • The boundaries are conventions, not cliffs. Nothing biological changes at 25.0 versus 24.9 — the 317-gram example above proves how arbitrary the line is at the individual level.
  • For children and teenagers, adult bands don't apply at all; paediatric BMI is read off age-and-sex percentile charts.

If you want to check your own numbers — including the distance to the nearest band boundary — the calculator I built for this does exactly that, with the bands shown alongside the result: BMI calculator.

The formula is 194 years old and one line long. The hard part was never the arithmetic. It's deciding what the number is allowed to claim — and building the UI so it claims no more than that.

Top comments (0)