Calculator program in JavaScript
Here you will learn to create a calculator program in javascript using HTML language.
A calculator is a program, which is used to perform the mathematical long calculations in few nano seconds.
Program code of Calculator program in JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <html> <body> <h2>JavaScript Calculator</h2> <form> First Number:<br> <input type="text" name="field1" value=""> <br> Second Number:<br> <input type="text" name="field2" value=""> <br> <input type="button" onClick="sum()" value="Addition"> <input type="button" onClick="sub()" value="Subtraction"> <input type="button" onClick="mul()" value="Multiply"> <input type="button" onClick="div()" value="Divide"> <br><br> Result: <br> <input type="text" name="result" value=""> </form> <script> function sum() { var num1 = new Number(document.forms[0].field1.value); var num2 = new Number(document.forms[0].field2.value); var result = num1 + num2; document.forms[0].result.value = result; } function sub() { var num1 = new Number(document.forms[0].field1.value); var num2 = new Number(document.forms[0].field2.value); var result = num1 - num2; document.forms[0].result.value = result; } function mul() { var num1 = new Number(document.forms[0].field1.value); var num2 = new Number(document.forms[0].field2.value); var result = num1 * num2; document.forms[0].result.value = result; } function div() { var num1 = new Number(document.forms[0].field1.value); var num2 = new Number(document.forms[0].field2.value); var result = num1 / num2; document.forms[0].result.value = result; } </script> </body> </html> |
Output
Check out our other JavaScript examples