Online JavaScript Calculator: Perform Mathematical Operations

15 Oct 2023 Balmiki Mandal 0 JavaScript

Calculator in JavaScript

JavaScript is a versatile programming language that can be used to create a wide variety of web applications, including calculators. A simple calculator application can be designed using JavaScript to perform basic arithmetic operations such as addition, subtraction, multiplication, and division.

To design a simple calculator application in JavaScript, we will need to use the following steps:

  1. Create an HTML file. The HTML file will contain the basic structure of the calculator, such as the input field, buttons, and output field.
  2. Add a CSS file. The CSS file will be used to style the calculator and make it look more appealing.
  3. Add a JavaScript file. The JavaScript file will contain the code to perform the calculations and update the output field.

Example of a simple calculator application in JavaScript:

HTML
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>

<h1>Calculator</h1>

<input type="text" id="display" value="0">

<button type="button" id="7">7</button>
<button type="button" id="8">8</button>
<button type="button" id="9">9</button>
<button type="button" id="/">/</button>

<button type="button" id="4">4</button>
<button type="button" id="5">5</button>
<button type="button" id="6">6</button>
<button type="button" id="*">*</button>

<button type="button" id="1">1</button>
<button type="button" id="2">2</button>
<button type="button" id="3">3</button>
<button type="button" id="-">-</button>

<button type="button" id="0">0</button>
<button type="button" id=".">.</button>
<button type="button" id="=">=</button>
<button type="button" id="c">C</button>

<script src="script.js"></script>

</body>
</html>
 
CSS
body {
  font-family: sans-serif;
}

#display {
  width: 200px;
  height: 50px;
  font-size: 20px;
  text-align: right;
}

button {
  width: 50px;
  height: 50px;
  font-size: 20px;
}
 
JavaScript
// Get the display element.
const display = document.getElementById('display');

// Add event listeners to the buttons.
document.querySelectorAll('button').forEach(button => {
  button.addEventListener('click', () => {
    // Update the display element with the button's text.
    display.value += button.textContent;
  });
});

// Add an event listener to the equal button.
document.getElementById('=').addEventListener('click', () => {
  // Evaluate the expression in the display element and update the output.
  display.value = eval(display.value);
});

// Add an event listener to the clear button.
document.getElementById('c').addEventListener('click', () => {
  // Clear the display element.
  display.value = '';
});

This is just a basic example of a calculator application in JavaScript. You can add more features, such as support for more complex mathematical operations, or create a more user-friendly interface.

Top Resources Now:


Enroll Now:


[JavaScript 2023-2024: From Scratch to Advanced] "Start Supercharging Your Productivity!"

Contact Us:


  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.