Use Increment Decrement Operators in Javascript

Description

The increment and decrement operators is represented by ++ and -- respectively.

The increment and decrement operators have two versions: prefix and postfix.

The prefix versions of the operators are placed before the variable, while the postfix ones are placed after the variable.

Example

To use a prefix increment, which adds 1 to a numeric value, you place two plus signs (++) in front of a variable like this:


var age = 29;
console.log(age);
++age;
console.log(age);

The code above is effectively equal to the following:


var age = 29;
console.log(age);
age = age + 1;
console.log(age);

The prefix decrement subtracts 1 from a numeric value. To use a prefix decrement, place two minus signs (--) before a variable, as shown here:


var age = 29;
console.log(age);
    --age;
console.log(age);

Note

When using either a prefix increment or a prefix decrement, the variable's value is changed before the statement is evaluated.


var age = 29; 
var anotherAge = --age + 2; 
console.log(age);         //outputs 28
console.log(anotherAge);  //outputs 30

Postfix

The postfix versions of increment and decrement use the same syntax (++ and --, respectively) but are placed after the variable.

For postfix increment and decrement operators, the increment or decrement occur after the containing statement has been evaluated.


var num1 = 2;
console.log(num1);//  ww w . j a v a 2  s.  c o m
var num2 = 20;
console.log(num2);
var num3 = num1-- + num2;    //equals 22
console.log(num3);
var num4 = num1 + num2;      //equals 21
console.log(num4);

The code above generates the following result.

Note 2

All four of these operators work on any values including integers, strings, Booleans, floating-point values, and objects.

The increment and decrement operators follow these rules regarding values:

  • For a string with a valid number representation, convert to a number and apply the change.
  • For a string with not-valid-number representation, the variable's value is set to NaN.
  • For a false Boolean value, convert to 0 and apply the change.
  • For a true Boolean value, convert to 1 and apply the change.
  • For a floating-point value, apply the change by adding or subtracting 1.
  • For an object, call its valueOf() method to get a value. Apply the other rules. If the result is NaN, then call toString() and apply the other rules again.

The following example demonstrates some of these rules:


var s1 = "3";
var s2 = "z";
var b = false;
var f = 1.1;
var o = {
    valueOf: function() {//from   w  w w  . j  a v a2 s.c om
        return -1;
    }
};

s1++;
console.log(s1);
s2++;
console.log(s2);
b++; 
console.log(b);
f--; 
console.log(f);
o--; 
console.log(o);

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions