BMI Calculator


You can create a BMI calculator using JavaScript. The formula for BMI(Body Mass Index) is as follows:


where weight is in kg and height in cm.

In the html page, you can create three text box using the <input> tag to accept the user inputs for name, height and weight.

In the JavaScript, you can create a function to calculate the bmi, as follows:

function bmi(){

var sheight=parseFloat(document.getElementById(‘hgt’).value);
var sweight=parseFloat(document.getElementById(‘wgt’).value);
var bmi=sweight/Math.pow(sheight,2);
var student_bmi=document.getElementById(‘bmi_index’);

  • toFixed method is to round the output to 2 decimal ppalces

The html document is

<!DOCTYPE html>

<h1>Student Name=<input type=”text” style=”font-size:25px; font-weight: bold;” id=”studentname”></h1>
<h2>Height=<input type=”text” style=”font-size:20px; font-weight: bold;” id=”hgt”></h2>
<h2>Weight=<input type=”text” style=”font-size:20px; font-weight: bold;” id=”wgt”></h2>
<h2>BMI=<b id=”bmi_index”></b></h2>

<input type=”button” value=”Calculate” onclick=”bmi()”>
<script type=”text/javascript” src=”bmi2.js”></script>

Click here to launch the BMI calculator