# How to create Body Mass Index: BMI calculator in js?

###### Minifycode 2021-10-03 Viewed 1.3K times Javascript

In this article, you will learn how to create BMI calculator in javascript?

BMI- Body mass index is defined as the body mass divided by the square of the body height and is expressed in units of kg/m2, resulting from mass in kilograms and height in meters.

``````
<title>Body Mass Index Calculator(BMI)</title>
<script type="text/javascript">
function calculateBMI()
{
var height=Number(document.getElementById("height").value);
var heightunits=document.getElementById("heightunits").value;
var weight=Number(document.getElementById("weight").value);
var weightunits=document.getElementById("weightunits").value;

if (heightunits=="inches") height/=39.3700787;
if (weightunits=="pounds") weight/=2.20462;

var BMI=weight/Math.pow(height,2);

document.getElementById("output").innerText=Math.round(BMI*100)/100;

var output =  Math.round(BMI*100)/100
if (output<18.5)
document.getElementById("comment").innerText = "Underweight";
else   if (output>=18.5 && output<=25)
document.getElementById("comment").innerText = "Normal";
else   if (output>=25 && output<=30)
document.getElementById("comment").innerText = "Obese";
else   if (output>30)
document.getElementById("comment").innerText = "Overweight";
}
</script>

<h1>Body Mass Index Calculator(BMI)</h1>
<p>Enter your height: <input type="text" id="height"/>
<select type="multiple" id="heightunits">
<option value="meters" selected="selected">meters</option>
<option value="inches">inches</option>
</select>
</p>
<p>Enter your weight: <input type="text" id="weight"/>
<select type="multiple" id="weightunits">
<option value="kilograms" selected="selected">kilograms</option>
<option value="pounds">pounds</option>
</select>
</p>
<input type="submit" value="calculateBMI" onclick="calculateBMI();">