Examples of Increment and Decrement Operators : Operator « Language Basics « JavaScript DHTML






Examples of Increment and Decrement Operators

  
<html>
<head>
  <title>JavaScript Unleashed</title>
</head>
<body>
   
  <script type="text/javaScript">
  <!--
    var i = 0;  
    var result = 0
   
    document.write("If i = 0, ");
    document.write("i returns the value of i");
    document.write(" after incrementing  : ");
   
    // Increment prefix
    result = ++i; 
   
    document.write(result);
    i = 0; 
    document.write("<br>i++ returns the value of i");
    document.write(" before incrementing : ");
   
    // Increment suffix
    result = i++; 
    document.write(result);
    i = 0; 
    document.write("<br>--i returns the value of i");
    document.write(" after decrementing  : ");
   
    // Decrement prefix
    result = --i; 
    document.write(result)
   
    i = 0;
    document.write("<br>i-- returns the value of i");
    document.write(" before decrementing : ");
       // Decrement suffix
    result = i--;
   
    document.write(result);
    // -->
  </script>
   
</body>
</html>



           
         
    
  








Related examples in the same category

1.Using the Equal Operator
2.Operators and Expressions:Operator Summary Table
3.Working with the Increment Operator
4.Add two integer variables together
5.Subtract one integer variable from another integer variable
6.Multiply two integer variables together
7.Modular operation (remainder in division)
8.Prefix plus
9.Prefix subtraction
10.Postfix adding
11.Postfix subtraction
12.Postfix operator for integer
13.Prefix operator for integer
14.JavaScript compound plus assignment
15.JavaScript compound subtraction assignment
16.JavaScript compound multiply assignment
17.JavaScript compound divide assignment
18.Ternary operator
19.Tenary operator