Simple JavaScript Calculator Tutorial: W3Schools

Sarah Jul 13, 2026

Creating a simple calculator using JavaScript is an excellent way to understand and apply basic JavaScript concepts. One of the foremost online learning resources for JavaScript is w3schools, which provides step-by-step guides for beginner and advanced developers alike. Let's dive into creating a simple calculator using JavaScript on w3schools.

Simple Calculator
Simple Calculator

w3schools has an interactive JavaScript tutorial that starts with the basics and gradually moves into more complex topics. We'll use this tutorial to guide us through creating a simple calculator that performs addition and subtraction operations.

Build A Calculator With JavaScript Tutorial
Build A Calculator With JavaScript Tutorial

Setting up the HTML Structure

Before we start coding, we need to set up the HTML structure for our calculator. We'll need an input field for the numbers, some buttons for the operations, and a display area for the results.

Simple JavaScript Calculator - Code Snippet
Simple JavaScript Calculator - Code Snippet

Here's the basic HTML structure:

```html

```

Accessing HTML Elements with JavaScript

How to create a simple JavaScript Calculator
How to create a simple JavaScript Calculator

In order to manipulate the HTML elements, we need to access them using JavaScript. We use the `getElementById()` method to access the elements by their `id` attributes.

Here's how you can access the input fields and buttons:

```javascript const num1 = document.getElementById('num1'); const num2 = document.getElementById('num2'); const operator = document.getElementById('operator'); const result = document.getElementById('result'); ```

Implementing the Calculation Function

Simple Calculator Program Using Javascript In Html
Simple Calculator Program Using Javascript In Html

Now that we have accessed our HTML elements, we can implement the `calculate()` function that performs the calculation based on the selected operator.

Here's the JavaScript code for the `calculate()` function:

```javascript function calculate() { const n1 = Number(num1.value); const n2 = Number(num2.value); const op = operator.value; let resultText; if (isNaN(n1) || isNaN(n2)) { resultText = 'Please enter valid numbers'; } else if (op === '+') { resultText = `Result: ${n1 + n2}`; } else if (op === '-') { resultText = `Result: ${n1 - n2}`; } else { resultText = 'Invalid operation'; } result.innerText = resultText; } ```

Now, when you enter two numbers, select an operator, and click the "Calculate" button, the result will be displayed below the calculator.

Calculator using HTML, CSS and JavaScript
Calculator using HTML, CSS and JavaScript

Expanding the Calculator Functionality

w3schools also provides a section on JavaScript arrays, which can help us expand our calculator to perform multiple operations with more than two numbers. We can use arrays to store the numbers and loop through them to perform calculations.

Loan Calculator in JavaScript
Loan Calculator in JavaScript
programming a simple calculator
programming a simple calculator
How To Make A Calculator Using HTML CSS And JavaScript || Create Calculator using JavaScript
How To Make A Calculator Using HTML CSS And JavaScript || Create Calculator using JavaScript
How to Building a JavaScript Calculator Project
How to Building a JavaScript Calculator Project
Calculator UI / webdevelopment / javascript
Calculator UI / webdevelopment / javascript
Calculator
Calculator
JavaScript Calculator || html css
JavaScript Calculator || html css
calculadora em javascript
calculadora em javascript
JavaScript calculator projet
JavaScript calculator projet
Calculator using html css and JavaScript
Calculator using html css and JavaScript
a calculator screen with the text,'how do i use this calculator? '
a calculator screen with the text,'how do i use this calculator? '
How to build an HTML calculator app from scratch using JavaScript
How to build an HTML calculator app from scratch using JavaScript
Build a Calculator using HTML CSS & JavaScript
Build a Calculator using HTML CSS & JavaScript
23K views | Reel by James Code Lab
23K views | Reel by James Code Lab
DATE OF BIRTH | Age Calculator Using JavaScript | Beginner
DATE OF BIRTH | Age Calculator Using JavaScript | Beginner
a computer screen with the text function calculator and an image of a keyboard
a computer screen with the text function calculator and an image of a keyboard
How to make a Calculator
How to make a Calculator
JavaScript Projeto - APP de Calculadora
JavaScript Projeto - APP de Calculadora
Simple Calculator
Simple Calculator
Responsive Calculator UI Design with HTML CSS JavaScript Free Download | Modern Calculator
Responsive Calculator UI Design with HTML CSS JavaScript Free Download | Modern Calculator

Adding Support for More Operations

By using JavaScript objects, we can add support for more operations like multiplication and division. Here's how you can modify the `calculate()` function to support more operations:

```javascript function calculate() { const n1 = Number(num1.value); const n2 = Number(num2.value); const op = operator.value; let resultText; if (isNaN(n1) || isNaN(n2)) { resultText = 'Please enter valid numbers'; } else { switch (op) { case '+': resultText = `Result: ${n1 + n2}`; break; case '-': resultText = `Result: ${n1 - n2}`; break; case '*': resultText = `Result: ${n1 * n2}`; break; case '/': if (n2 !== 0) { resultText = `Result: ${n1 / n2}`; } else { resultText = 'Cannot divide by zero'; } break; default: resultText = 'Invalid operation'; } } result.innerText = resultText; } ```

Now, the calculator supports addition, subtraction, multiplication, and division operations.

Using JavaScript Arrays to Perform Calculations with More Than Two Numbers

To perform calculations with more than two numbers, we can modify our HTML structure to include a text area for the user to input numbers separated by commas. We can then use the `split()` method to convert the input into an array of strings, and the `map()` method to convert the strings into numbers.

Here's the modified HTML structure:

```html

```

And here's the modified `calculate()` function:

```javascript function calculate() { const numbers = document.getElementById('numbers').value.split(',').map(Number); const op = operator.value; let resultText; if (isNaN(numbers[0])) { resultText = 'Please enter valid numbers'; } else { let result = numbers[0]; for (let i = 1; i < numbers.length; i++) { switch (op) { case '+': result += numbers[i]; break; case '-': result -= numbers[i]; break; case '*': result *= numbers[i]; break; case '/': if (numbers[i] !== 0) { result /= numbers[i]; } else { resultText = 'Cannot divide by zero'; break; } break; default: resultText = 'Invalid operation'; break; } } resultText = `Result: ${result}`; } result.innerText = resultText; } ```

Now, the calculator can perform calculations with more than two numbers. The user just needs to enter the numbers separated by commas and select an operator.

Creating a simple calculator using JavaScript is an excellent way to apply what you've learned from the w3schools JavaScript tutorial. As you continue exploring more complex JavaScript concepts, you can build upon this simple calculator to create even more impressive projects.

Happy coding, and don't forget to check out w3schools for more JavaScript tutorials and exercises!