Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Temperature Converter using HTML CSS and JavaScript
Next article icon

Design a BMI Calculator using JavaScript

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 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

HTML
<!-- index.html file  --> <!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8" />     <meta name="viewport" content="width=device-width, initial-scale=1.0" />     <title>BMI Calculator</title> </head>  <body style="background-color: rgb(244, 255, 244)">     <div class="container">         <h1>BMI Calculator</h1>          <!-- Option for providing height              and weight to the user-->         <p>Height (in cm)</p>          <input type="text" id="height" />          <p>Weight (in kg)</p>          <input type="text" id="weight" />          <button id="btn">Calculate</button>          <div id="result"></div>     </div> </body>  </html> 
JavaScript
window.onload = () => {     let button = document.querySelector("#btn");      // Function for calculating BMI     button.addEventListener("click", calculateBMI); };  function calculateBMI() {      /* Getting input from user into height variable.     Input is string so typecasting is necessary. */     let height = parseInt(document         .querySelector("#height").value);      /* Getting input from user into weight variable.      Input is string so typecasting is necessary.*/     let weight = parseInt(document         .querySelector("#weight").value);      let result = document.querySelector("#result");      // Checking the user providing a proper     // value or not     if (height === "" || isNaN(height))         result.innerHTML = "Provide a valid Height!";      else if (weight === "" || isNaN(weight))         result.innerHTML = "Provide a valid Weight!";      // If both input is valid, calculate the bmi     else {          // Fixing upto 2 decimal places         let bmi = (weight / ((height * height)             / 10000)).toFixed(2);          // Dividing as per the bmi conditions         if (bmi < 18.6) result.innerHTML =             `Under Weight : <span>${bmi}</span>`;          else if (bmi >= 18.6 && bmi < 24.9)             result.innerHTML =                 `Normal : <span>${bmi}</span>`;          else result.innerHTML =             `Over Weight : <span>${bmi}</span>`;     } } 

Output:



Next Article
Temperature Converter using HTML CSS and JavaScript
author
imsushant12
Improve
Article Tags :
  • CSS
  • HTML
  • JavaScript
  • Technical Scripter
  • Web Technologies
  • JavaScript-Questions
  • Technical Scripter 2020

Similar Reads

  • HTML Calculator
    HTML calculator is used for performing basic mathematical operations like Addition, subtraction, multiplication, and division. You can find the live preview below, try it: To design the HTML calculator, we will use HTML, and CSS. HTML is used to design the basic structure of the calculator. CSS styl
    2 min read
  • JavaScript Calculator
    To build a simple calculator using JavaScript, we need to handle basic arithmetic operations such as addition, subtraction, multiplication, and division. JavaScript, along with HTML and CSS, is commonly used to create interactive web-based calculators. What We Are Going to CreateWe will build a simp
    7 min read
  • JavaScript Scientific Calculator
    The HTML Scientific Calculator is a tool for performing advanced scientific calculations like finding exponents, logarithms, factorials, and more. This calculator comprises two sections: the input section, where the user types in their mathematical problem, and the output screen, which displays all
    4 min read
  • JavaScript Neumorphism Effect Calculator
    In this article, we will learn how to create a working calculator with the Neumorphism effect using HTML, CSS, and JavaScript. Basic mathematical operations such as addition, subtraction, multiplication, and division can be performed using this calculator. Approach: Neumorphism is a contemporary app
    3 min read
  • JavaScript Age Calculator
    In Age Calculator, we will take the date of birth as the date input and it prints the age from the current date (or specified date). We will create the structure of the Age Calculator using HTML and CSS, and JavaScript will add the functionality to calculate the age in years, months, and days. Appro
    3 min read
  • JavaScript Tip Calculator
    The tip is the money given as a gift for good service, to the person who serves you in a restaurant. In this project, a simple tip calculator is made which takes the billing amount, type of service, and the number of persons as input. As per the three inputs, it generates a tip for the serving perso
    4 min read
  • JavaScript Geometry Calculator
    In this article, we will see how to design a Geometry Calculator using HTML, CSS, and JavaScript. A Geometry calculator is used to calculate the area and parameters of different shapes and figures, like circles, rectangles, squares, triangles, etc. This can be helpful in cases where one wants to cal
    4 min read
  • JavaScript Aspect Ratio Calculator
    In this article, we are going to implement an aspect ratio calculator. An aspect ratio calculator proves to be a useful tool for individuals seeking to determine the proportions of images or videos based on their width and height. Our aspect ratio calculator has a live preview option that enables us
    5 min read
  • JavaScript Binary Calculator
    HTML or HyperText Markup Language along with CSS (Cascading Stylesheet) and JavaScript can be used to develop interactive user applications that can perform certain functionalities. Similarly, a binary calculator can be developed using HTML, CSS, and JS altogether. Binary Calculator performs arithme
    5 min read
  • JavaScript Percentage Calculator
    The percentage calculator is useful for students, shopkeepers, and for solving basic mathematical problems related to percentages. In this article, we are going to learn, how to make a percentage calculator using HTML CSS, and JavaScript Formula used:What is X percent of Y is given by the formula: X
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences