Skip to content

Commit

Permalink
Merge pull request loverajoel#268 from megapctr/tip-35
Browse files Browse the repository at this point in the history
Describe double plus/minus operators. Closes loverajoel#223
  • Loading branch information
zenopopovici committed Feb 22, 2016
2 parents 7ec0641 + 247fefd commit 9043061
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions _posts/en/2016-02-04-assignment-shorthands.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ n --; n = n - 1;

````

### `++` and `--` operators

There is a special `++` operator. It's best to explain it with an example:

````javascript
var a = 2;
var b = a++;
// Now a is 3 and b is 2
````

The `a++` statement does this:
1. return the value of `a`
2. increment `a` by 1

But what if we wanted to increment the value first? It's simple:

````javascript
var a = 2;
var b = ++a;
// Now both a and b are 3
````

See? I put the operator _before_ the variable.

The `--` operator is similar, except it decrements the value.

### If-else (Using ternary operator)

This is what we write on regular basis.
Expand Down

0 comments on commit 9043061

Please sign in to comment.