Thursday, 28 February 2013

JavaScript : Arithmetic Special Operators (%, ++, --, - )


Arithmetic Special Operators

In addition to four standard arithmetic operators (+, -, *, /), JavaScript provides the following arithmetic operators.

JavaScript Modulus operator (%)

The modulus operator is used as follows:
var1 % var2
The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the above statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. The result will have the same sign as var1.

Example :

HTML Code

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

JS Code

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

Practice the example online

JavaScript Increment operator (++ )

The increment operator is used as follows:
  • var1++    ( Post-increment )
  • ++var1    ( Pre-increment )
In the first case (i.e. post-increment) the operator increases the variable var1 by 1, but returns the value before incrementing.
In the second case (i.e. pre-increment) the operator increases the variable var1 by 1, but returns the value after incrementing.

Example :

HTML Code

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

JS Code

  1. var var1 = 45;  
  2. var X = 45;  
  3.   
  4. var newParagraph = document.createElement("p");   
  5. var newText = document.createTextNode("Initial value of var1 = "+var1+" and X = "+X);  
  6. newParagraph.appendChild(newText);  
  7. document.body.appendChild(newParagraph);  
  8.   
  9. var newParagraph1 = document.createElement("p");  
  10. var newText1 = document.createTextNode("Lets apply the following fourmula X = var1++ [ i.e. Post increment of a variable ]");   
  11. newParagraph1.appendChild(newText1);   
  12. document.body.appendChild(newParagraph1);   
  13.   
  14. X=var1++;  
  15.   
  16. var newParagraph2 = document.createElement("p");  
  17. var newText2 = document.createTextNode("After post-increment value of var1 = "+var1+" and X = "+X);   
  18. newParagraph2.appendChild(newText2);   
  19. document.body.appendChild(newParagraph2);   
  20.   
  21. var var1 = 45;  
  22. var X = 45;  
  23.   
  24. var newParagraph3 = document.createElement("p");   
  25. var newText3 = document.createTextNode("Initial value of var1 = "+var1+" and X = "+X);   
  26. newParagraph3.appendChild(newText3);   
  27. document.body.appendChild(newParagraph3);   
  28.   
  29. var newParagraph4 = document.createElement("p");   
  30. var newText4 = document.createTextNode("Lets apply the following fourmula X =++var1 [ i.e. Pre increment of a variable ]");   
  31. newParagraph4.appendChild(newText4);   
  32. document.body.appendChild(newParagraph4);   
  33. X=++var1;  
  34.   
  35. var newParagraph5 = document.createElement("p");   
  36. var newText5 = document.createTextNode("After pre-increment value of var1 = "+var1+" and X = "+X); //creates text along with ouput to be displayed   
  37. newParagraph5.appendChild(newText5);   
  38. document.body.appendChild(newParagraph5);   

Practice the example online

JavaScript : Decrement operator (--)

The decrement operator is used as follows:
  • var1--   ( Post-decrement )
  • --var1   ( Pre-decrement )
In the first case (i.e. post-decrement) the operator decreases the variable var1 by 1, but returns the value before decrementing.
In the second case (i.e. pre-decrement) the operator decreases the variable var1 by 1, but returns the value after decrementing.

Example :

HTML Code

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

JS Code

  1. var var1 = 45;  
  2. var X = 0;  
  3.   
  4. var newParagraph = document.createElement("p");  
  5. var newText = document.createTextNode("Initial value of var1 = "+var1+" and X = "+X);  
  6. newParagraph.appendChild(newText);  
  7. document.body.appendChild(newParagraph);  
  8.   
  9. var newParagraph1 = document.createElement("p");  
  10. var newText1 = document.createTextNode("Lets apply the following fourmula X = var1-- [ i.e. Post decrement of a variable ]");   
  11. newParagraph1.appendChild(newText1);  
  12. document.body.appendChild(newParagraph1);  
  13. X=var1--;  
  14.   
  15. var newParagraph2 = document.createElement("p");   
  16. var newText2 = document.createTextNode("After post-decrement value of var1 = "+var1+" and X = "+X);  
  17. newParagraph2.appendChild(newText2);  
  18. document.body.appendChild(newParagraph2);   
  19.   
  20. var var1 = 45;  
  21. var X = 0;  
  22.   
  23. var newParagraph3 = document.createElement("p");  
  24. var newText3 = document.createTextNode("Initial value of var1 = "+var1+" and X = "+X);  
  25. newParagraph3.appendChild(newText3);  
  26. document.body.appendChild(newParagraph3);   
  27.   
  28. var newParagraph4 = document.createElement("p");   
  29. var newText4 = document.createTextNode("Lets apply the following fourmula X = --var1 [ i.e. Pre decrement of a variable ]");   
  30. newParagraph4.appendChild(newText4);   
  31. document.body.appendChild(newParagraph4);  
  32. X=--var1;  
  33.   
  34. var newParagraph5 = document.createElement("p");   
  35. var newText5 = document.createTextNode("After pre-decrement value of var1 = "+var1+" and X = "+X);   
  36. newParagraph5.appendChild(newText5);  
  37. document.body.appendChild(newParagraph5);  

Practice the example online

JavaScript : - Unary Negation operator

-var1
The unary negation operator changes the sign of var1. When the operator negating a variable, the value of the variable remains unchanged, but return value is negated.

Example :

HTML Code

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

JS Code

  1. var var1 = 45;  
  2. var X = 0;  
  3. var newParagraph = document.createElement("p");  
  4. var newText = document.createTextNode('Initial value of var1 = '+var1+' and X = '+X);  
  5. newParagraph.appendChild(newText);  
  6. document.body.appendChild(newParagraph);  
  7.   
  8. var newParagraph1 = document.createElement("p");  
  9. var newText1 = document.createTextNode('Lets apply the unary negation operator in var1 i.e.  X = -var1');  
  10. newParagraph1.appendChild(newText1);  
  11. document.body.appendChild(newParagraph1);  
  12.   
  13. X=-var1;  
  14. var newParagraph2 = document.createElement("p");  
  15. var newText2 = document.createTextNode('After negating the value of var1 = '+var1+' and X ='+X);   
  16. newParagraph2.appendChild(newText2);  
  17. document.body.appendChild(newParagraph2);  

Practice the example online


No comments:

Post a Comment