CourseCommunityPricingBoilerplates
Get Started

Products

CourseBoilerplatesPricing

Resources

CommunityGitHubYouTube

Contribute

Write for UsStyle Guide

Legals

Privacy PolicyTerms of ServiceContact Us
©2025TheDevSpace.io| All Rights Reserved
Table of Contents
  • Creating the calculator layout
  • Adding styles
  • Adding scripts
  • appendNumber()
  • calculate()
  • clearDisplay()
  • A challenge

Building a Calculator with JavaScript

<Regex Related MethodsNetwork & HTTP>

In the previous chapter on HTML and CSS, we created a calculator. It has the right layout and look, but doesn't function. After spending so much time discussing JavaScript, we are finally ready to fix that problem.

calculator

Creating the calculator layout

As usual, let's start by creating the HTML skeleton.

html
1<div class="calculator">
2  <div class="display">0</div>
3  <div class="buttons">
4    <div class="button function">AC</div>
5    <div class="button function">+/-</div>
6    <div class="button function">%</div>
7    <div class="button function">/</div>
8
9    <div class="button number">7</div>
10    <div class="button number">8</div>
11    <div class="button number">9</div>
12    <div class="button function">*</div>
13
14    <div class="button number">4</div>
15    <div class="button number">5</div>
16    <div class="button number">6</div>
17    <div class="button function">-</div>
18
19    <div class="button number">1</div>
20    <div class="button number">2</div>
21    <div class="button number">3</div>
22    <div class="button function">+</div>
23
24    <div class="button number">0</div>
25    <div class="button function">.</div>
26    <div class="button function">=</div>
27  </div>
28</div>
  • .calculator is a container that wraps around the whole calculator.
  • .display shows the user input, as well as the output of the calculation.
  • .buttons contains both the .number buttons (0-9) and the .function buttons (+, -, *...).

And, of course, you must set up the event listeners so that when a button is clicked, the corresponding JavaScript function is executed.

html
1<div class="calculator">
2  <div class="display" id="display">0</div>
3  <div class="buttons">
4    <div class="button function" onclick="clearDisplay()">AC</div>
5    <div class="button function" onclick="toggleSign()">+/-</div>
6    <div class="button function" onclick="percent()">%</div>
7    <div class="button function" onclick="appendOperator('/')">/</div>
8
9    <div class="button number" onclick="appendNumber('7')">7</div>
10    <div class="button number" onclick="appendNumber('8')">8</div>
11    <div class="button number" onclick="appendNumber('9')">9</div>
12    <div class="button function" onclick="appendOperator('*')">*</div>
13
14    <div class="button number" onclick="appendNumber('4')">4</div>
15    <div class="button number" onclick="appendNumber('5')">5</div>
16    <div class="button number" onclick="appendNumber('6')">6</div>
17    <div class="button function" onclick="appendOperator('-')">-</div>
18
19    <div class="button number" onclick="appendNumber('1')">1</div>
20    <div class="button number" onclick="appendNumber('2')">2</div>
21    <div class="button number" onclick="appendNumber('3')">3</div>
22    <div class="button function" onclick="appendOperator('+')">+</div>
23
24    <div class="button number" id="Num0" onclick="appendNumber('0')">0</div>
25    <div class="button function" onclick="appendOperator('.')">.</div>
26    <div class="button function" onclick="calculate()">=</div>
27  </div>
28</div>

Before we start coding, we also need to discuss the general structure of this project.

First of all, there should be a global variable (displayValue) to store the displayed value, as well as a helper function (updateDisplay()) that updates the .display based on the variable. The function selects the display element and updates its content.

Wait, there is more!

Please create an account to access this content.

From HTML & CSS to JavaScript, from basic language syntaxes to web development frameworks, from the frontend to the backend, we'll guide you through every step of your coding journey.

🎉 Create an Account 🎉
<Regex Related MethodsNetwork & HTTP>

Subscribe below to grab our free Full-Stack Web Developer Starter Kit 👇

Also follow us onorwhere we share coding tips daily.

Course Outline
Introduction+
  • 1.WebDev Fundamentals
HTML & CSS+
  • 1.Preparations
  • 2.HTML Elements
  • 3.Query Selectors
  • 4.Colors
  • 5.Typography
  • 6.Text Customization
  • 7.Text Spacing & Alignment
  • 8.Functions & At Rules
  • 9.Links
  • 10.Lists
  • 11.Tables
  • 12.Forms
  • 13.Media Files
  • 14.The Box Model
  • 15.Display Types
  • 16.Overflow
  • 17.Object Fit
  • 18.Aspect Ratio
  • 19.Float
  • 20.Position
  • 21.The Column Layout
  • 22.The Grid Layout
  • 23.The Flexbox Layout
  • 24.Justify & Align
  • 25.Z index
  • 26.Visibility & Opacity
  • 27.Box Shadow
  • 28.Backgrounds
  • 29.Gradient
  • 30.Blend Modes
  • 31.Filters
  • 32.Transforms
  • 33.Transitions
  • 34.Animations
  • 35.Cursor & Scroll Behavior
  • 36.Responsive Design
  • 37.Responsive Media & Text
  • 38.Responsive Layout
  • 39.Best Practices
  • 40.Project #1: Recreating YouTube
  • 41.Project #2: Calculator Part 1
TailwindCSS+
  • 1.Introducing TailwindCSS
JavaScript+
  • 1.Introduction
  • 2.Setting Up a Dev Environment
  • 3.Basic Syntax
  • 4.Data Types
  • 5.Numbers & BigInt
  • 6.Strings
  • 7.Boolean Values
  • 8.Undefined & Null
  • 9.Type Conversion
  • 10.Arrays
  • 11.Objects
  • 12.Maps & Sets
  • 13.If Statements
  • 14.Switch Statements
  • 15.While Loops
  • 16.For Loops
  • 17.Functions
  • 18.Variables & Scope
  • 19.Rest Parameter & Spread Syntax
  • 20.Pure Functions
  • 21.Higher Order Functions
  • 22.Recursion
  • 23.Methods
  • 24.Constructor Functions
  • 25.Getters & Setters
  • 26.Prototypes
  • 27.The Class Notation
  • 28.Class Inheritance
  • 29.Static Properties
  • 30.Private Properties
  • 31.Object Oriented Programming
  • 32.Error Handling
  • 33.Asynchronous Programming
  • 34.Promise
  • 35.Async & Wait
  • 36.JavaScript Modules
  • 37.The DOM Tree
  • 38.Manipulation the DOM
  • 39.Event Handling
  • 40.Some Common Events
  • 41.Canvas
  • 42.Project #3: Image Slider
  • 43.Project #4: Drawing Board
  • 44.Working with Date
  • 45.The Math Object
  • 46.JSON
  • 47.Regular Expressions
  • 48.Define Patterns with Regex
  • 49.Regex Related Methods
  • 50.Project #5: Calculator Part 2
Express.js+
  • 1.Network & HTTP
  • 2.A Basic Web App
  • 3.Express.js
  • 4.Model Layer
  • 5.View Layer
  • 6.Controller Layer
  • 7.Form Handling
  • 8.Uploading Files
  • 9.Project #6: Blog
  • 10.Loading Static Files
  • 11.ORM Integration
  • 12.Adding Relations
  • 13.Middleware
  • 14.Browser Data Storage
  • 15.User Authentication
  • 16.REST API
  • 17.Best Practices
  • 18.Going to Production
React.js+
  • 1.Fundamentals
  • 2.JSX
  • 3.Conditional Rendering
  • 4.List Rendering
  • 5.Components
  • 6.Props
  • 7.Adding Styles
  • 8.Event Handling
  • 9.Introducing State
  • 10.State Management
  • 11.Form Handling
  • 12.Reducer
  • 13.Context
  • 14.Refs
  • 15.Effects
  • 16.Hooks
  • 17.Rules of React
  • 18.Project #7: Todo List
  • 19.Project #8: Weather App
  • 20.Project #9: Blog
  • 21.React.js Optimization
Next.js+
  • 1.Next.js Basics
  • 2.Routing
  • 3.Pages & Layout
  • 4.Server vs. Client Components
  • 5.API Routes
  • 6.Server Actions
  • 7.Database Integration
  • 8.Data Fetching
  • 9.Error Handling
  • 10.Middleware
  • 11.Links, Navigation & Redirection
  • 12.Images
  • 13.Scripts
  • 14.Fonts
  • 15.Lazy Loading
  • 16.Caching
  • 17.Loading UI
  • 18.Project #10: SaaS
  • 19.User Authentication
  • 20.Magic Link
  • 21.Custom Emails
  • 22.Protecting Routes
  • 23.OAuth Providers
  • 24.Dashboard
  • 25.Role Based Access Control
  • 26.Payment Integration
  • 27.Pricing Page
  • 28.Stripe Checkout
  • 29.Payment Webhook
Miscellaneous+
  • 1.Optimize Your Web App
Recent Community Articles
  • How to Build Interactive Forms Using HTML and CSS
  • How to Create a Modern App with Django and Vue
  • How to Send HTTP Requests Using JavaScript
  • Vue.js Fundamentals
  • How to Reverse a String in JavaScript
  • How to Reverse an Array in JavaScript
  • JavaScript Fundamentals 2025
  • How to Implement Pagination with JavaScript
  • Laravel Fundamentals
  • Django Fundamentals