Learn to create a functional calculator using HTML, CSS, and JavaScript!
Think about a real physical calculator:
We're going to build the EXACT same thing, but on a webpage!
Every calculator needs these three parts working together:
1. The Display (Output Screen)
<input> element set to readonly2. The Buttons (Input Interface)
<button> elements arranged in a grid3. The Logic (Brain)
When you type "5 + 3 =" on a calculator, here's what happens:
The key insight: We build the calculation as a TEXT STRING first, then evaluate it!
// What the display contains at each step:
Step 1: "5"
Step 2: "5+"
Step 3: "5+3"
Step 4: Evaluate "5+3" → Result is 8
How do we arrange buttons in a grid pattern?
We'll use CSS Grid, which is like creating a spreadsheet layout:
Grid Template Columns:
display: grid;
grid-template-columns: repeat(4, 1fr);
What this means:
display: grid; = "Use grid layout"repeat(4, 1fr) = "Create 4 equal-width columns"1fr = "1 fraction" (each column gets equal space)Visual representation:
Column1 Column2 Column3 Column4 [7] [8] [9] [/] [4] [5] [6] [*] [1] [2] [3] [-] [0] [.] [=] [+]
Items automatically flow into the grid left-to-right, top-to-bottom!
How do we make buttons DO something when clicked?
We use the onclick attribute:
<button onclick="appendToDisplay('5')">5</button>
Breaking it down:
onclick = "When this button is clicked..."appendToDisplay = Name of the JavaScript function to run('5') = Pass the value '5' to the functionWhen user clicks the button:
onclick attributeappendToDisplay('5')How does JavaScript calculate "5+3*2"?
We use the eval() function:
const expression = "5+3*2";
const result = eval(expression);
console.log(result); // Shows: 11
What eval() does:
Examples:
eval("2+2") = 4eval("10-5") = 5eval("3*4") = 12eval("10/2") = 5eval("5+3*2") = 11 (multiplication happens first!)⚠️ Important note: In real-world apps, eval() can be dangerous with user input from untrusted sources. But in our calculator, WE control what gets evaluated (only numbers and operators), so it's safe!
Why use readonly?
<input type="text" id="display" value="0" readonly>
The readonly attribute means:
Why is this important?
appendToDisplay(value)
clearDisplay()
calculate()
We'll build this calculator in 6 progressive steps:
💡 By the end, you'll have a real calculator that works just like your phone's calculator app!
Goal: Create the calculator's display screen where numbers and results will show.
Use a <div> with an <input> element to create the display.
<input type="text"> with readonly so users can't type directly into it. The calculator will update it programmatically!
Goal: Create buttons for numbers 0-9.
Add a <div> container with number buttons in a grid layout.
grid-template-columns: repeat(3, 1fr) to create a 3-column layout. The buttons will automatically wrap to create rows!
Goal: Add buttons for mathematical operations (+, -, *, /).
Add operation buttons with different styling to distinguish them from numbers.
operator to style operation buttons differently (orange looks good!).
Goal: Add the Clear (C) and Equals (=) buttons to complete the button layout.
The Clear button should span multiple columns and the equals button should stand out.
grid-column: span 3 to make the Clear button take 3 columns, and grid-column: span 2 for the equals button. Different colors help: red for clear, green for equals!
Goal: Make the calculator functional with JavaScript!
Add onclick attributes to buttons and create functions to handle clicks.
appendToDisplay() - Adds number/operator to displayclearDisplay() - Resets display to '0'calculate() - Uses eval() to compute resultFinal Challenge: Put it all together - a complete, working calculator with all features!
The calculator should handle all operations, clear, and display results correctly.