How to Build a Calculator, BMI Calculator and Age Calculator Using HTML, CSS and JavaScript Beginner Friendly
How I Built a Calculator, BMI Calculator and Age Calculator Using HTML, CSS and JavaScript
When you are starting as a frontend or full stack developer, it is very easy to jump directly into frameworks and complex tools and skip the fundamentals. However, small, focused projects in plain HTML, CSS and JavaScript are still one of the most powerful ways to build real skills and a solid portfolio.
Try it yourself: Calculator
In this article, I am sharing the process, thinking and learning behind a mini collection of projects I recently completed:
- A basic but fully functional Calculator
- A simple BMI (Body Mass Index) Calculator
- An Age Calculator
All three are built using only HTML, CSS and Vanilla JavaScript. I also focused heavily on the user interface, using a glass-like blurred card design with a background image. This was not only a JavaScript practice exercise, but also a UI design and layout exercise.
If you are searching for:
- How to build a calculator using HTML, CSS and JavaScript
- How to create a BMI calculator in JavaScript
- How to make an age calculator using JavaScript
- What is glassmorphism UI and how to implement it
this article will walk you through the concepts, structure and approach that I used, in a way that is easy to adapt for your own projects.
Project Overview and Live Demo
The three tools sit inside a simple multi-page setup, where each page focuses on a specific feature. The main idea was to keep each feature focused, but still make the whole experience feel like one small application.
You can explore the live project here:
Live demo: Calculator, BMI and Age Tools
Why Start With a Calculator, BMI Calculator and Age Calculator
These three ideas may look basic, but they are very powerful learning tools.
1. Calculator: Practising Core JavaScript Logic
A calculator is a classic beginner project and there is a good reason for that. It forces you to think about:
- Storing and updating state such as the current value, previous value and selected operator
- Handling user input for numbers and decimal points
- Performing operations like addition, subtraction, multiplication and division
- Updating the screen without reloading the page
Once you start writing the logic, you realise that even a simple calculator has many small edge cases. For example:
- What happens if the user presses the operator multiple times
- What if the user presses the equals button repeatedly
- How to prevent the user from entering multiple decimal points in one number
These questions force you to build a proper mental model of how the calculator should behave and then translate that behaviour into JavaScript.
2. BMI Calculator: Real World Formula and Validation
A BMI calculator is a small but realistic example of how many web applications work. Users enter some values, the application applies a formula and the result is shown on the screen.
In a BMI calculator, users usually input:
- Weight (for example in kilograms)
- Height (for example in centimetres or metres)
The JavaScript logic then:
- Validates that the values are numbers and not empty
- Converts height into the correct unit if required
- Applies the BMI formula
- Displays the result to the user, possibly with a small interpretation such as underweight, normal, or overweight
This is a very common pattern in real applications where the browser acts as a small calculator or assistant for the user.
3. Age Calculator: Working With Dates and Simple Arithmetic
The age calculator starts from a very simple idea: enter your birth year (and optionally the current year), and see your age. It sounds trivial, but it still reinforces key skills:
- Reading user input from form fields
- Checking for invalid or empty values
- Performing arithmetic operations on those values
- Showing a clear and readable result
Age calculators are also a good starting point for learning to work with dates, although this project keeps it simple by focusing on years rather than full date objects.
The Technology Stack: HTML, CSS and Vanilla JavaScript
The entire project uses only:
- HTML for structure
- CSS for layout and visual design
- Plain JavaScript for logic and interactivity
There are no frameworks, no libraries and no build tools. This is intentional. When you are learning, it is important to understand how the browser works with raw HTML, how CSS affects elements on the page and how JavaScript manipulates the DOM.
Designing the Glassmorphism User Interface

One of the most satisfying parts of this project was designing the user interface. Instead of a plain background, I chose a high quality image and placed a semi transparent, slightly blurred card on top of it. This creates a modern glass-like effect often referred to as glassmorphism.
The effect is achieved using a combination of:
- A background image set on the
body - A central container with a transparent background using
rgba backdrop-filter: blur(...)and the vendor prefixed version-webkit-backdrop-filter- Rounded corners and a soft border to enhance the glass feel
Here is a simplified example of the kind of CSS used for the card container:
.divmid {
margin: 50px;
height: 300px;
width: 400px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
gap: 30px;
background: rgba(255, 255, 255, 0.18);
border-radius: 16px;
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
backdrop-filter: blur(10.6px);
-webkit-backdrop-filter: blur(10.6px);
border: 1px solid rgba(255, 255, 255, 0.33);
}
This combination creates a transparent panel that picks up the background image behind it and gives the impression of a frosted glass layer. Inputs and buttons are styled to match, with rounded edges and subtle contrast.
Working on this UI was not just a visual exercise. It also required understanding layout, centering, spacing, and responsive behaviour, which are all essential frontend skills.
How the Calculator Works in JavaScript
The core of the calculator is its JavaScript logic. At a high level, the calculator keeps track of:
- The current number being typed by the user
- The first number entered before an operator is chosen
- The operator selected by the user, such as plus or minus
- Whether the next input should reset the display or continue appending
To manage this, I used variables like:
currentValueto store what the user is typingfirstValueto store the previous number when an operator is pressedcurrentOperatorto remember which operator is activeshouldResetto decide whether the next number starts fresh or continues the existing one
For example, when a number button is clicked, the clickedNumber function is called:
function clickedNumber(num) {
if (shouldReset) {
currentValue = "";
shouldReset = false;
}
currentValue = currentValue + num;
updateDisplay(currentValue);
}
This function appends digits to the current value and updates the display. The shouldReset flag is used to clear the previous result when the user starts typing a new number after an operation.
The dot or decimal button requires special handling. The calculator should not allow multiple decimal points in a single number. To handle this, I used a simple condition:
function clickedDot() {
if (shouldReset) {
currentValue = "";
shouldReset = false;
}
if (!currentValue.includes(".")) {
if (currentValue === "") {
currentValue = "0.";
} else {
currentValue = currentValue + ".";
}
updateDisplay(currentValue);
}
}
This ensures that a dot is only added if the current value does not already contain one.
When an operator button is pressed, the setOperator function decides whether to store the current value as the first value or to compute an intermediate result if both values already exist:
function setOperator(op) {
if (currentValue === "" && firstValue === null) {
// do nothing if there is no value yet
} else {
if (firstValue === null) {
firstValue = parseFloat(currentValue);
} else if (!shouldReset && currentValue !== "") {
let secondValue = parseFloat(currentValue);
operate(firstValue, secondValue, currentOperator);
firstValue = finalResult;
updateDisplay(finalResult);
}
currentOperator = op;
shouldReset = true;
}
}
The actual arithmetic is handled by a separate operate function:
function operate(a, b, op) {
if (op === "+") {
finalResult = a + b;
} else if (op === "-") {
finalResult = a - b;
} else if (op === "*") {
finalResult = a * b;
} else if (op === "/") {
finalResult = a / b;
}
}
Separating the calculation logic from the event handling code keeps the structure clearer and easier to modify later.
How the BMI Calculator Works
The BMI calculator follows a straightforward flow:
- The user enters height and weight into input fields.
- The JavaScript function attached to the button click reads those values.
- The code validates that the values are not empty and not invalid.
- The BMI is calculated and displayed inside a result container.
A typical structure for this in HTML looks like this:
<input type="number" id="weightInput" placeholder="Enter weight in kg">
<input type="number" id="heightInput" placeholder="Enter height in cm">
<button onclick="calculateBMI()">Calculate BMI</button>
<div id="bmiResult"></div>
And a simple JavaScript function might look like:
function calculateBMI() {
var weight = document.getElementById("weightInput").value;
var height = document.getElementById("heightInput").value;
var result = document.getElementById("bmiResult");
if (weight === "" || height === "") {
result.textContent = "Please enter both height and weight";
return;
}
weight = Number(weight);
height = Number(height) / 100; // convert cm to m
if (weight <= 0 || height <= 0) {
result.textContent = "Please enter valid values";
return;
}
var bmi = weight / (height * height);
result.textContent = "Your BMI is " + bmi.toFixed(2);
}
This is not complex code, but it covers several important ideas:
- Accessing DOM elements using
document.getElementById - Working with string values and converting them to numbers
- Using simple arithmetic in JavaScript
- Displaying results dynamically without reloading the page
How the Age Calculator Works
The age calculator follows a similar pattern. The user enters a birth year, and the JavaScript calculates the age based on a current year.
For example, a very simple version would be:
<input type="number" id="birthInput" placeholder="Enter your birth year">
<input type="number" id="currentInput" placeholder="Enter current year">
<button onclick="calculateAge()">Get Age</button>
<div id="ageOutput"></div>
And the JavaScript logic:
function calculateAge() {
var birthYear = document.getElementById("birthInput").value;
var currentYear = document.getElementById("currentInput").value;
var output = document.getElementById("ageOutput");
if (birthYear === "" || currentYear === "") {
output.textContent = "Please enter both years";
return;
}
birthYear = Number(birthYear);
currentYear = Number(currentYear);
if (isNaN(birthYear) || isNaN(currentYear) || birthYear <= 0 || currentYear <= 0) {
output.textContent = "Please enter valid years";
return;
}
var age = currentYear - birthYear;
output.textContent = "Your age is " + age + " years";
}
This reinforces input handling, validation and basic calculations, while still being user friendly.
Key JavaScript Concepts Practised in This Project
While these tools are small, they cover many important JavaScript concepts that every frontend developer should be comfortable with:
- Using
document.getElementByIdto access DOM elements - Reading and updating
textContentto show results on the page - Handling button click events through
onclickattributes and functions - Working with numbers and converting input values from strings to numeric types
- Using conditional statements to control flow based on user input
- Organising logic into functions that are easy to reuse and understand
At the same time, the UI work reinforces core CSS skills:
- Flexbox for centering content vertically and horizontally
- Using gaps and margins to create clean spacing
- Applying border radius and shadows for a modern look
- Implementing glassmorphism using
backdrop-filterand transparency
Why These Types of Projects Are Valuable for Beginners
If you are a beginner developer or a fresher preparing for internships and entry level roles, these kinds of projects are extremely useful. They are small enough to finish quickly, but rich enough to talk about in interviews and to include in your portfolio.
They help you answer questions like:
- How do you handle user input in JavaScript
- How do you update the screen when a button is clicked
- What is the difference between working with strings and numbers
- How do you structure your JavaScript code across multiple functions
- How do you design a clean, modern frontend layout with HTML and CSS
They also show potential employers that you have taken the time to practise real code, not just read tutorials. A working calculator or BMI calculator with a polished UI demonstrates initiative, attention to detail and a desire to improve.
Conclusion
Building a calculator, a BMI calculator and an age calculator using HTML, CSS and JavaScript might seem simple on the surface, but in reality it touches a wide range of important frontend skills. From state management and user input to validation and UI design, these mini projects act as a compact training ground for real world development.
If you are looking for ideas on how to practise JavaScript and frontend development, I strongly recommend starting with projects like these. Focus on both logic and design, pay attention to details like decimal input and edge cases, and do not be afraid to spend extra time refining the layout and visual style.
Over time, these small steps add up to strong skills and a portfolio that you can confidently share with others.
You can try the live version of my project here:
Live demo: HTML, CSS and JavaScript Calculator, BMI Calculator and Age Calculator
I hope this breakdown helps you understand what is involved in creating these tools and gives you ideas for your own projects.
Comments
Post a Comment