Skip to content

Simple calculator program in JavaScript using functions

  • by

Learn how to create a simple calculator program in JavaScript using functions with this tutorial. This code example demonstrates how to define and use functions for basic arithmetic operations like addition, subtraction, multiplication, and division in JavaScript.

Simple calculator program in JavaScript using functions example

Simple example code.

<!DOCTYPE html>
<html>
  <body>
    <script>
        function add(num1, num2) {
            return num1 + num2;
        }

        function subtract(num1, num2) {
            return num1 - num2;
        }

        function multiply(num1, num2) {
            return num1 * num2;
        }

        function divide(num1, num2) {
            return num1 / num2;
        }

        // Example usage:
        console.log(add(2, 3)); 
        console.log(subtract(5, 2)); 
        console.log(multiply(4, 6)); 
        console.log(divide(10, 2)); 

    </script>
  </body>
</html>

Output:

Simple calculator program in JavaScript using functions

Do comment if you have any doubts or suggestions on this JS code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Tags:

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading