JAVASCRIPT PROJECT 09

Build a JavaScript BMI Calculator

In this project, you will learn how to collect user input, perform mathematical calculations, validate values and update the webpage using JavaScript.

01

Project Introduction

A BMI Calculator calculates Body Mass Index from a person's weight and height.

What You Will Learn

  • HTML form inputs
  • JavaScript variables
  • Number conversion
  • Mathematical calculations
  • Functions
  • Conditional statements
  • DOM manipulation
  • Input validation
Weight Enter weight in kilograms.
Height Enter height in centimeters.
Result Calculate and display BMI.
02

Create the HTML Structure

First create two input fields for weight and height, followed by Calculate and Reset buttons.

<input
    type="number"
    id="weight"
    placeholder="Weight in kg"
>

<input
    type="number"
    id="height"
    placeholder="Height in cm"
>

<button onclick="calculateBMI()">
    Calculate BMI
</button>

<button onclick="resetBMI()">
    Reset
</button>

<div id="result">
    Your BMI will appear here.
</div>
The id attribute allows JavaScript to find and control these HTML elements.
03

Get User Input

Use getElementById() to access the values entered by the user.

const weight =
    document.getElementById("weight").value;

const height =
    document.getElementById("height").value;

Input values are initially received as strings. We need numbers for mathematical calculations.

const weight =
    Number(
        document.getElementById("weight").value
    );

const height =
    Number(
        document.getElementById("height").value
    );
04

Understand the BMI Formula

BMI can be calculated using weight in kilograms and height in meters.

BMI = weight / (height × height)

Convert Centimeters to Meters

const heightInMeters =
    height / 100;

JavaScript Formula

const bmi =
    weight /
    (heightInMeters * heightInMeters);
05

Create the Calculate Function

Put the calculation inside a function so it can be called when the user clicks the button.

function calculateBMI(){

    const weight =
        Number(
            document.getElementById("weight").value
        );

    const height =
        Number(
            document.getElementById("height").value
        );

    const heightInMeters =
        height / 100;

    const bmi =
        weight /
        (heightInMeters * heightInMeters);

    console.log(bmi);

}
Functions keep your JavaScript organized and reusable.
06

Validate the Input

Before calculating BMI, make sure that both values are valid and greater than zero.

if(
    weight <= 0 ||
    height <= 0
){

    alert(
        "Please enter valid values."
    );

    return;

}

Why Validation Is Important

  • Prevents empty calculations.
  • Prevents zero values.
  • Prevents negative values.
  • Gives the user useful feedback.
07

Create BMI Categories

Conditional statements can be used to classify the calculated BMI.

let category;

if(bmi < 18.5){

    category = "Underweight";

}
else if(bmi < 25){

    category = "Normal range";

}
else if(bmi < 30){

    category = "Overweight";

}
else{

    category = "Obesity";

}
These ranges are commonly used as a general BMI classification example. The calculator is an educational JavaScript project, not a medical diagnosis tool.
08

Display the Result with DOM

Use textContent to display the calculated BMI and category inside the result element.

const result =
    document.getElementById("result");

result.textContent =
    "BMI: " +
    bmi.toFixed(2) +
    " - " +
    category;

Why toFixed(2)?

BMI calculations can produce many decimal places. toFixed(2) keeps the result readable by displaying two decimal places.

09

Create the Reset Function

Reset should clear the input fields and remove the old result.

function resetBMI(){

    document.getElementById("weight").value = "";

    document.getElementById("height").value = "";

    document.getElementById("result").textContent =
        "Your BMI will appear here.";

}

Complete Project Flow

INPUT Weight
HEIGHT Height
CALCULATE BMI
CHECK Category
RESULT Display
11

Complete BMI Calculator

You have now learned how to collect user input, convert values into numbers, perform calculations, validate data, classify the result and update the webpage using JavaScript.

Tutorial Completed! 🧮

Now open the complete working BMI Calculator and test everything you have learned.

Run Complete Project Learn JavaScript Operators