Skip to main content

JavaScript - Simple Basic Calculator


In this tutorial we will create a Simple Basic Calculator using JavaScript. This code automatically generate a calculator app when the user run the web application. The code itself use onclick() function to initiate a several method by means of computing a number when you pass a value as parameter.. You can apply this script to your code, it is a user-friendly program feel free to modify it.

We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer.
.

Getting Started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Simple Basic Calculator</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6 well" style="background-color:green;">
  17. <form name="calculator">
  18. <input type="text" id="result" style="width:100%; height:50px; font-size:30px;">
  19. <br /><br />
  20. <button type="button" onclick="calculate('1')" style="padding:25px; margin-left:10px; font-size:14px;">1</button>
  21. <button type="button" onclick="calculate('2')" style="padding:25px; font-size:14px;" >2</button>
  22. <button type="button" onclick="calculate('3')" style="padding:25px; font-size:14px;">3</button>
  23. <button type="button" onclick="calculate('+')" style="padding:25px; font-size:14px;">+</button>
  24. <br /><br />
  25. <button type="button" onclick="calculate('4')" style="padding:25px; margin-left:10px; font-size:14px;">4</button>
  26. <button type="button" onclick="calculate('5')" style="padding:25px; font-size:14px;">5</button>
  27. <button type="button" onclick="calculate('6')" style="padding:25px; font-size:14px;">6</button>
  28. <button type="button" onclick="calculate('-')" style="padding-top:25px; padding-bottom:25px; padding-right:27px; padding-left:27px; font-size:14px;">-</button>
  29. <br /><br />
  30. <button type="button" onclick="calculate('7')" style="padding:25px; margin-left:10px; font-size:14px;">7</button>
  31. <button type="button" onclick="calculate('8')" style="padding:25px; font-size:14px;">8</button>
  32. <button type="button" onclick="calculate('9')" style="padding:25px; font-size:14px;">9</button>
  33. <button type="button" onclick="calculate('*')" style="padding-top:25px; padding-bottom:25px; padding-right:26px; padding-left:26px; font-size:13px;">x</button>
  34. <br /><br />
  35. <button type="button" onclick="cancel();" style="padding:25px; margin-left:10px; font-size:14px;">C</button>
  36. <button type="button" onclick="calculate('0')" style="padding:25px; font-size:14px;">0</button>
  37. <button type="button" onclick="solve()" style="padding:25px; font-size:14px;">=</button>
  38. <button type="button" onclick="calculate('/')" style="padding-top:25px; padding-bottom:25px; padding-right:26px; padding-left:26px; font-size:14px;">/</button>
  39. </form>
  40. </div>
  41. <script src="js/script.js"></script>
  42. </body>
  43. </html>

Creating the Script

This code contains the script of the application. This code will calculate your equation when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory

  1. function calculate(val){
  2. document.getElementById("result").value+=val;
  3. }
  4.  
  5. function cancel(){
  6. document.getElementById("result").value='';
  7. }
  8.  
  9. function solve(){
  10. var number = document.getElementById("result").value;
  11. var total = eval(number);
  12. document.getElementById("result").value = total;
  13. }

There you have it we successfully created a Simple Basic Calculator using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.