Javascript - Simple Calculator

Submitted by: 
Language: 
Visitors have accessed this post 589 times.

In this tutorial we will create a Simple Calculator using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any mathematical problem. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/

The Main Function

This code contains the main function of the application. This code is the source code of the calculator that display the designed of the application. This will calculate the equation when the equal button is pressed. To create this just write these block of code inside the text editor.

  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="<a href="https://sourcecodester.com">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">Javascript - Simple Calculator</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <div class="col-md-1"></div>
  20. <div class="col-md-10 well" style="background-color:#ccc;">
  21. <form name="calculator">
  22. <input type="text" name="result" style="width:100%; height:50px; font-size:30px;">
  23. <br /><br />
  24. <button type="button" onclick="calculator.result.value+='1'" style="padding:25px; margin-left:5px; font-size:14px;">1</button>
  25. <button type="button" onclick="calculator.result.value+='2'" style="padding:25px; font-size:14px;" >2</button>
  26. <button type="button" onclick="calculator.result.value+='3'" style="padding:25px; font-size:14px;">3</button>
  27. <button type="button" onclick="calculator.result.value+='+'" style="padding:25px; font-size:14px;">+</button>
  28. <br /><br />
  29. <button type="button" onclick="calculator.result.value+='4'" style="padding:25px; margin-left:5px; font-size:14px;">4</button>
  30. <button type="button" onclick="calculator.result.value+='5'" style="padding:25px; font-size:14px;">5</button>
  31. <button type="button" onclick="calculator.result.value+='6'" style="padding:25px; font-size:14px;">6</button>
  32. <button type="button" onclick="calculator.result.value+='-'" style="padding-top:25px; padding-bottom:25px; padding-right:27px; padding-left:27px; font-size:14px;">-</button>
  33. <br /><br />
  34. <button type="button" onclick="calculator.result.value+='7'" style="padding:25px; margin-left:5px; font-size:14px;">7</button>
  35. <button type="button" onclick="calculator.result.value+='8'" style="padding:25px; font-size:14px;">8</button>
  36. <button type="button" onclick="calculator.result.value+='9'" style="padding:25px; font-size:14px;">9</button>
  37. <button type="button" onclick="calculator.result.value+='*'" style="padding-top:25px; padding-bottom:25px; padding-right:26px; padding-left:26px; font-size:13px;">x</button>
  38. <br /><br />
  39. <button type="button" onclick="calculator.result.value =''" style="padding:25px; margin-left:5px; font-size:14px;">C</button>
  40. <button type="button" onclick="calculator.result.value+='0'" style="padding:25px; font-size:14px;">0</button>
  41. <button type="button" onclick="calculator.result.value=eval(calculator.result.value)" style="padding:25px; font-size:14px;">=</button>
  42. <button type="button" onclick="calculator.result.value+='/'" style="padding-top:25px; padding-bottom:25px; padding-right:26px; padding-left:26px; font-size:14px;">/</button>
  43. </form>
  44. </div>
  45. </div>
  46. </div>
  47. </body>
  48. </html>

There you have it we successfully created a Simple 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!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.