How to do basic Maths with JavaScript Operators?

JavascriptWeb DevelopmentFront End Technology

To do basic Maths such as +, -, * in JavaScript, use the Operators. JavaScript includes operators for basic mathematics such as Addition, Subtraction, Multiplication, etc.

Example

You can try to run the following code to learn how to work with basic Maths i.e. Mathematical Operators in JavaScript −

<html>
   <body>
      <script>
         var a = 77;
         var b = 30;
         var c = "Demo";
         var linebreak = "<br />";
         document.write("a + b = ");
         result = a + b;
         
         document.write(result);
         document.write(linebreak);
         document.write("a - b = ");
         result = a - b;

         document.write(result);
         document.write(linebreak);
         document.write("a / b = ");
         result = a / b;

         document.write(result);
         document.write(linebreak);
         document.write("a % b = ");
         result = a % b;
         
         document.write(result);
         document.write(linebreak);
         document.write("a + b + c = ");
         result = a + b + c;

         document.write(result);
         document.write(linebreak);
         a = ++a;
         document.write("++a = ");
         result = ++a;

         document.write(result);
         document.write(linebreak);
         b = --b;
         document.write("--b = ");
         
         result = --b;
         document.write(result);
         document.write(linebreak);
      </script>
   </body>
</html>
raja
Published on 16-Feb-2018 12:07:32
Advertisements