DEV Community

Aakash Ahmed
Aakash Ahmed

Posted on

1 1

Create simple calculator in javascript

A simple calculator in javascript and html. This is the basic simple calculator in javascript which is made using a simple class in javascript. It also demonstrates how you can create a class for simple tasks and instantiate it.

HTML

<div id="calc">
    <input type="number" id="num1" placeholder="Enter a number"/>
    <select id="operator" >
        <option value="+">+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
    </select>
    <input type="number" id="num2" placeholder="Enter a number" />
    <button id="calc">=</button>
    <input type="number" id="result" placeholder="Result" />
</div>
Enter fullscreen mode Exit fullscreen mode

Javascript

class Calculator{
  // Properties

  num1
  num2
  result

  num1Input
  num2Input
  resultInput
  operatorSelect

  // member functions

    constructor(){
        // initialization of the calculator
    }

    setEvents(){
        // set events on button
    }

    add(){
        // add numbers
    }
    subtract(){
        // subtract numbers
    }
    multiple(){
        // multiple numbers
    }
    divide(){
        // divide numbers
    }

    output(){
        // output the results
    }
}
Enter fullscreen mode Exit fullscreen mode

For detailed tutorial on (this code) simple calculator in javascript, please follow this link:
https://10code.dev/javascript/a-simple-calculator-in-vanilla-javascript/

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay