Back to Projects

JS Simple Calculator

A browser-based calculator built with HTML, CSS, and JavaScript for basic arithmetic operations with dark mode support.

2023

Duration

Jul 2023

Contributors

1 contributor

Technologies

3+ tech

JS Simple Calculator

A calculator web app written in plain HTML, CSS and JavaScript. It handles the four basic operations, shows the full expression as you type, and includes clear and delete controls and a dark mode — all without ever calling eval().

Try the live demo.

The interesting part of a simple calculator#

A calculator looks like the simplest possible app, and the obvious implementation is one line: collect the button presses into a string and pass it to eval(). That line is also the problem. eval() executes arbitrary code, accepts malformed input, and hides every edge case behind whatever JavaScript happens to do with it.

This project does the work explicitly instead, which is where the edge cases become visible — and handled.

What it does#

  • Basic arithmetic — addition, subtraction, multiplication and division
  • Expression display — the full current input, plus the result
  • Clear (AC) and delete (⌫) — start over or remove the last input
  • Responsive design — sized for both desktop and mobile
  • Dark mode — a low-light theme

How it works#

State, not strings#

The calculator keeps a small state object — the current operand, the previous operand and the pending operator — and every button press is a transition on that state. In simplified form:

js
const state = { previous: null, current: '0', operator: null };

function chooseOperator(op) {
  if (state.operator && state.current !== '') compute();
  state.previous = state.current;
  state.operator = op;
  state.current = '';
  render();
}

Because operands and the operator are kept apart, the input never has to be parsed back out of a string. The display is always rendered from the state, so what you see is exactly what will be computed.

Edge cases, handled on purpose#

  • Consecutive operators — operator sequences are validated, so pressing two operators in a row can't produce an invalid expression.
  • Division by zero — caught before computing, with a user-friendly message on the display rather than Infinity.
  • Corrections — AC resets the state, and ⌫ removes the last input without starting over.

Dark mode#

Colours are CSS custom properties, and the dark theme redefines them under a single class on the root element, so the toggle is one line of JavaScript.

Results#

  • A live demo on GitHub Pages
  • A readable implementation in plain JavaScript, without eval()
  • Common edge cases like division by zero handled gracefully
Share:Share on Twitter / XShare on LinkedInShare on Facebook
MIT LicenseView on GitHub
MIT License

Copyright (c) 2023 Ismail ZAHIR

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

More Projects

Dr Maroc
2024Current
A healthcare SaaS live at drmaroc.com, grown from a single app into a platform: four Angular portals in one Nx monorepo, a modular Spring Boot backend, self-hosted Keycloak, shared contracts, and a delivery pipeline where GitHub is the source of truth.
AngularNxNext.js+8
ngx-auth-client
2026Current
A published npm package that exposes Angular authentication as signals instead of getters, ships Keycloak and HTTP adapters behind a provider-agnostic port, and keeps tokens out of localStorage entirely.
AngularTypeScriptKeycloak+7