How to Build a BMI Calculator in JavaScript - Complete Tutorial
Learn how to build a fully functional BMI Calculator using vanilla JavaScript. This step-by-step tutorial covers form validation, BMI calculation logic, localStorage for history, and responsive design.
How to Build a BMI Calculator in JavaScript
Building a BMI (Body Mass Index) Calculator is an excellent beginner project that teaches you essential JavaScript concepts including DOM manipulation, form validation, and local storage.
What You'll Learn
- •Form input handling and validation
- •Mathematical calculations in JavaScript
- •Working with localStorage API
- •Conditional rendering based on BMI categories
- •Building a responsive user interface
Project Overview
Our BMI Calculator will have these features:
- •Height and weight input with validation
- •Instant BMI calculation
- •Color-coded health category display
- •History of previous calculations
- •Delete individual history items
- •Fully responsive design
Step 1: HTML Structure
First, let's create the basic HTML structure:
<div class="container">
<h1>BMI Calculator</h1>
<form id="bmi-form">
<input type="number" id="height" placeholder="Height (cm)" required>
<input type="number" id="weight" placeholder="Weight (kg)" required>
<button type="submit">Calculate BMI</button>
</form>
<div id="result"></div>
<div id="history"></div>
</div>Step 2: BMI Calculation Logic
The BMI formula is: BMI = weight (kg) / (height (m))²
function calculateBMI(weight, height) {
const heightInMeters = height / 100;
const bmi = weight / (heightInMeters * heightInMeters);
return bmi.toFixed(1);
}Step 3: Determine Health Category
function getCategory(bmi) {
if (bmi < 18.5) return { category: 'Underweight', color: '#3b82f6' };
if (bmi < 25) return { category: 'Normal', color: '#10b981' };
if (bmi < 30) return { category: 'Overweight', color: '#f59e0b' };
return { category: 'Obese', color: '#ef4444' };
}Step 4: Form Validation
form.addEventListener('submit', (e) => {
e.preventDefault();
const height = parseFloat(document.getElementById('height').value);
const weight = parseFloat(document.getElementById('weight').value);
if (!height || !weight || height <= 0 || weight <= 0) {
alert('Please enter valid values');
return;
}
const bmi = calculateBMI(weight, height);
displayResult(bmi);
saveToHistory(bmi, weight, height);
});Step 5: LocalStorage Integration
function saveToHistory(bmi, weight, height) {
const history = JSON.parse(localStorage.getItem('bmiHistory')) || [];
history.unshift({
bmi,
weight,
height,
date: new Date().toLocaleDateString()
});
localStorage.setItem('bmiHistory', JSON.stringify(history));
displayHistory();
}Why This Project is Great for Beginners
- Real-world Application - BMI calculators are actually used
- Multiple Concepts - Covers forms, validation, storage
- Visual Feedback - Color-coded results make it engaging
- Portfolio-Ready - Looks professional with minimal styling
Try It Yourself
Next Steps
After completing this project, try:
- •Adding metric/imperial unit conversion
- •Creating charts to visualize BMI trends
- •Adding user authentication
- •Building a mobile app version
Happy coding! 🚀
🚀Build These Projects
Put what you learned into practice with these hands-on projects
📚Continue Learning
Explore more tutorials and guides
50+ JavaScript Interview Questions & Answers (2026) — Beginner to Advanced
50+ most asked JavaScript interview questions with clear, detailed answers. From beginner basics to advanced concepts like closures, event loop, prototypes, and async patterns — everything you need to crack your next JS interview.
How to Build a Weather App in JavaScript - Complete Fetch API Tutorial
Build a real-time Weather App with JavaScript from scratch. Learn the Fetch API, async/await error handling, Geolocation API, and how to integrate OpenWeatherMap in this complete step-by-step tutorial.
DOM Manipulation JavaScript Hindi: Complete Guide with Real Examples
DOM Manipulation JavaScript ka sabse important concept hai! Is guide mein seekho getElementById, querySelector, createElement, Events - sab kuch Hindi mein real examples ke saath.