DEV Community

Cover image for Build a BMI Calculator Using JavaScript – Try It Now!
Random coder
Random coder

Posted on

Build a BMI Calculator Using JavaScript – Try It Now!

Maintaining a healthy weight is crucial for overall well-being, and one of the easiest ways to track your fitness level is by calculating your Body Mass Index (BMI). BMI is a numerical value derived from your height and weight, helping you understand if you're underweight, normal weight, overweight, or obese.

To make this process effortless, we've created a simple and efficient BMI calculator that you can use online. Try it now on our webpage: Randzy BMI Calculator.

What is BMI and Why Is It Important?

BMI is a widely used health metric that categorizes individuals into different weight groups:

Underweight: BMI < 18.5

Normal Weight: BMI 18.5 - 24.9

Overweight: BMI 25 - 29.9

Obese: BMI 30+

While BMI is a useful indicator, it doesn't directly measure body fat. However, it serves as a good starting point for assessing general health.

How to Create a BMI Calculator Using JavaScript

If you're interested in building a BMI calculator, here's a step-by-step guide using HTML, CSS, and JavaScript.

  1. Create the HTML Structure

Let's start with a basic form where users can input their weight and height:

<!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>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <h1>BMI Calculator</h1>
        <label for="height">Height (in meters):</label>
        <input type="number" id="height" step="0.01" required>

        <label for="weight">Weight (in kg):</label>
        <input type="number" id="weight" required>

        <button onclick="calculateBMI()">Calculate BMI</button>

        <p id="result"></p>
    </div>
    <script src="script.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
  1. Style It with CSS

A simple CSS file (styles.css) can improve the visual appeal of your BMI calculator:

body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f5f5;
}
.container {
    background: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
    text-align: center;
}
input {
    display: block;
    width: 100%;
    padding: 8px;
    margin: 10px 0;
}
button {
    background-color: #28a745;
    color: white;
    padding: 10px;
    border: none;
    cursor: pointer;
}
button:hover {
    background-color: #218838;
}
Enter fullscreen mode Exit fullscreen mode
  1. Implement the JavaScript Logic

The following JavaScript function calculates the BMI and displays the result:

function calculateBMI() {
    const height = document.getElementById("height").value;
    const weight = document.getElementById("weight").value;

    if (height === "" || weight === "") {
        alert("Please enter both height and weight!");
        return;
    }

    const bmi = (weight / (height * height)).toFixed(2);
    let message = "";

    if (bmi < 18.5) {
        message = "Underweight";
    } else if (bmi >= 18.5 && bmi < 24.9) {
        message = "Normal weight";
    } else if (bmi >= 25 && bmi < 29.9) {
        message = "Overweight";
    } else {
        message = "Obese";
    }

    document.getElementById("result").innerHTML = `Your BMI is ${bmi} (${message})`;
}
Enter fullscreen mode Exit fullscreen mode
  1. Test Your BMI Calculator

Now, open the HTML file in your browser and enter your height and weight to check your BMI.

Try Our Online BMI Calculator

Rather than coding from scratch, you can use our fully functional and accurate BMI calculator online at Randzy BMI Calculator. It's simple, easy to use, and gives instant results!

Conclusion

Building a BMI calculator with JavaScript is a great beginner-friendly project that introduces form handling, event listeners, and DOM manipulation. Whether you're coding your own or using Randzy's BMI Calculator, keeping track of your BMI is an essential step toward a healthier lifestyle.

Have fun coding and stay healthy!

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed
  • 2:34 --only-changed
  • 4:27 --repeat-each
  • 5:15 --forbid-only
  • 5:51 --ui --headed --workers 1

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

Top comments (0)

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay