Javascript Increment ++ and Decrement — operators

Javascript Increment ++ and Decrement — operators

Increment and Decrement operators are just short cuts for addition and subtraction.
But the idea is with the increment operator you only add 1 to a number
With the Decrement Operator you only subtract 1 or take away 1 from a number.
The increment operator looks like this ++ or 2 plus signs.
The decrement operator looks like thisor 2 minus signs.

The Increment Operator ++

To Increment a number means to add 1 to it.

// Create a variable 
var myNumber = 10;

// Add 1 to it without the increment operator or the long way 
myNumber = myNumber + 1;

console.log(myNumber);  // Returns 11 cuz 10 + 1 = 11

// now that was the long way 

Now let’s add 1 using the Increment operator

Usually without the increment operator you do it the long way like in the example above.
But with the increment operator we can shorten it like so:

var myNumber = 10;

// we can use just ++ 
myNumber++;
console.log(myNumber);  // returns 11

The Decrement Operator —

To Decrement a number means to subtract 1 or take away 1 from a number.

// Create a variable 
var myNumber = 10;

// subtract 1 from a number without the decrement operator or the long way 
myNumber = myNumber - 1;

console.log(myNumber);  // Returns 9 cuz 10 - 1 = 9

// now that was the long way 

Now let’s subtract 1 using the Decrement operator

Usually without the decrement operator you do it the long way like in the example above.
But with the decrement operator we can shorten it like so:

var myNumber = 10;

// we can use just --
myNumber--;
console.log(myNumber);  // returns 11

Watch my Javascript Increment and Decrement operators

I also make videos for my written Javascript tutorials.
If you are a visual person I highly recommend watching this video.

Comments are closed.