VOOZH about

URL: https://www.geeksforgeeks.org/javascript/design-a-bmi-calculator-using-javascript/

⇱ Design a BMI Calculator using JavaScript - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Design a BMI Calculator using JavaScript

Last Updated : 18 Apr, 2025

A BMI (Body Mass Index) Calculator measures body fat based on weight and height, providing a numerical value to categorize individuals as underweight, normal weight, overweight, or obese. It’s widely used to assess health risks and guide lifestyle or medical decisions.

A BMI Calculator using JavaScript allows users to input their weight and height, then calculates and displays their Body Mass Index (BMI), helping assess whether they are underweight, normal weight, or overweight.

Formula:

BMI = (weight) / (height * height) 
// height in cms and weight in kgs

Example:

  • Weight: 70 kg
  • Height: 170 cm
Height in meters = 170 cm / 100 = 1.7 m
BMI = 70 / (1.7 * 1.7)
BMI = 70 / 2.89
BMI β‰ˆ 24.22

Output Preview:

πŸ‘ Image
Image Preview

Approach

BMI is a number calculated from an individual's weight and height.

  • Collect height and weight from the user, stored in `height` and `weight` variables.
  • Compute BMI by dividing weight (kg) by the square of height (mΒ²).
  • Use if-else statements to interpret the BMI result and determine the category.
  • Check for empty inputs; if any are missing, display messages to provide height or weight.
  • Use HTML for structure, CSS for styling, and JavaScript for input processing and output display.

In the JavaScript section, we are processing the taken input and after calculating, the respective output is printed.

Example: In this example, we will structure in the index.html file and implement the logic in the app.js file

Output:

πŸ‘ Image

Comment