Thursday, 28 February 2013

JavaScript : Arithmetic Operators


Arithmetic Operators : +, -, *, /

In JavaScript, arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. There are four standard  arithmetic operators, addition (+), subtraction (-), multiplication (*), and division (/).
These operators work as they do in other programming languages except the division (/) operator which returns a floating-point division in JavaScript, not a truncated division as it does in languages such as C or Java.

For example:
1/2  returns 0.5 in JavaScript.
1/2  returns 0 in Java.
In addition, JavaScript provides modules(%), increment(++), decrement(--) and unary negation(-) operators.

Example : ( + addition operator)

HTML Code

  1. <!doctype html>  
  2. <head>  
  3. <meta charset="utf-8">  
  4. <title>JavaScript + operator example</title>  
  5. <meta name="description" content="This document contains  
  6. an example of JavaScript + operator" />  
  7. </head>  
  8. <body>  
  9. <script src="addition_example1.js"></script>  
  10. </body>  
  11. </html>  

JS Code

  1. var var1 = 45;  
  2. var var2 = 78;  
  3. var var3 = 45.10;  
  4. var var4 = 178.12;  
  5. var newvar  = var1 + var2;  
  6. var newvar1 = var3 + var4;  
  7. var newParagraph3 = document.createElement("p");  
  8. var newText3 = document.createTextNode("var1 + var2 = "+newvar+" and var3 + var4 = "+newvar1);  
  9. newParagraph3.appendChild(newText3);  
  10. document.body.appendChild(newParagraph3);  

Practice the example online

Example : ( - subtraction operator)

HTML Code

  1. <!doctype html>  
  2. <head>  
  3. <meta charset="utf-8">  
  4. <title>JavaScript subtraction operator example </title>  
  5. <meta name="description" content="This document contains an  
  6. example Using JavaScript subtraction operator"/>  
  7. </head>  
  8. <script src="subtraction-example1.js"></script>  
  9. </body>  
  10. </html>  

JS Code

  1. var var1 = 45;  
  2. var var2 = 78;  
  3. var str1 = "w3resource";  
  4. var str2 = ".com";  
  5. var newvar = var1 - var2;  
  6. var newstr = str1 - str2;  
  7. var varstr = var1 - str2;  
  8. var newParagraph = document.createElement("p");  
  9. var newText = document.createTextNode("var1 -var2 = "+ newvar);  
  10. newParagraph.appendChild(newText);  
  11. document.body.appendChild(newParagraph);  
  12. var newParagraph1 = document.createElement("p");  
  13. var newText1 = document.createTextNode("str1 - str2  = "+ newstr);  
  14. newParagraph1.appendChild(newText1);  
  15. document.body.appendChild(newParagraph1);  
  16. var newParagraph2 = document.createElement("p");  
  17. var newText2 = document.createTextNode("var1 - str2  = "+ varstr);  
  18. newParagraph2.appendChild(newText2);  
  19. document.body.appendChild(newParagraph2)  

Practice the example above online

Example : ( * multiplication operator)

HTML Code

  1. <!doctype html><head>  
  2. <meta charset="utf-8">  
  3. <title>JavaScript multiplication operator (*) example with DOM  
  4. </title>  
  5. <meta name="description" content="This document contains an example  
  6. Using JavaScript multiplication operator (*) with dom"/>  
  7. </head>  
  8. <body>  
  9. <script src="multiplication-example1.js"></script>  
  10. </body>  
  11. </html>  

JS Code

  1. var var1 = 45;  
  2. var var2 = 78;  
  3. var var3 = 45.10;  
  4. var var4 = 178.12;  
  5. var newvar  = var1 * var2;  
  6. var newvar1 = var3 * var4;  
  7.   
  8. var newParagraph = document.createElement("p");  
  9. var newText = document.createTextNode("var1 * var2 = "+ newvar);  
  10. newParagraph.appendChild(newText);  
  11. document.body.appendChild(newParagraph);  
  12.   
  13. var newParagraph1 = document.createElement("p");  
  14. var newText1 = document.createTextNode("var3 * var4  = "+ newvar1);  
  15. newParagraph1.appendChild newText1);  
  16. document.body.appendChild(newParagraph1);  

Practice the example online

Example : ( / division operator)

HTML Code

  1. <!doctype html>  
  2. <head>  
  3. <meta charset="utf-8">  
  4. <title>JavaScript / operator example </title>  
  5. <meta name="description" content="This document contains an example of JavaScript division operator" />  
  6. </style>  
  7. </head>  
  8. <body>  
  9. <script src="division-example1.js"></script>  
  10. </body>  
  11. </html>   

JS Code

  1. var var1 = 45;  
  2. var var2 = 78;  
  3. var var3 = 1;  
  4. var var4 = 2;  
  5. var newvar  = var1 / var2;  
  6. var newvar1 = var3 / var4;  
  7.   
  8. var newParagraph = document.createElement("p");  
  9. var newText = document.createTextNode ("var1 / var2 = "+ newvar);  
  10. newParagraph.appendChild(newText);  
  11. document.body.appendChild(newParagraph);  
  12.   
  13. var newParagraph1 = document.createElement("p");  
  14. var newText1 = document.createTextNode("var3 / var4  = "+ newvar1);  
  15. newParagraph1.appendChild(newText1);  
  16. document.body.appendChild(newParagraph1);  

Practice the example online

No comments:

Post a Comment