JavaScript increment and decrement

First, the increment ++

It can be made variable by the increment operator on the basis of its own plus one;

For subsequent increment a variable, the value of the original variable will increase immediately from a;

Increment symbol: ++

Increment divided into two types: 1, after ++ (a ++); 2, before ++ (++ a);

Thing in common: A ++ and ++ a variable for the original is no difference, the value of the original variables are incremented by one.

Different points: different values and a ++ a ++ a.

A ++ a value equal to the value of the original variable (the value before the increment).

++ a value equal to the new value increase from the original variable.

 Example:

a ++ is based on the original value incremented (a = 20, a ++ = 20);

++ a is based on the self-energizing time after the then incremented (++ a = 22);

increment equivalent to twice a (a = 20 + 1 + 1 = 22);

Why a ++ + ++ a + a = 20 + 22 + 22 = 64

 

Second, since the reduction - -

By decrementing the variable can be in its own on the basis of a decrease;

Minus symbol from : -

Decrement divided into two types : 1, - - (a -); 2 , pre - - (- a);

The same point : a-- and --a will cause the value of the original variable immediately from minus one;

Differences : different values of a-- and --a.

a- - is the original value of the variable (the value before decrementing);

--a is the new value of the original variable (decrementing value);

 

 Example:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>JS</title>
 6 </head>
 7 <body>
 8     <script>
 9     var a=10,b=20;
10     var c=a++;
11     console.log("a="+a);
12     console.log("c="+c);
13     c=++a;
14     console.log("a="+a);
15     console.log("c="+c);
16     c=b--;
17     console.log("c="+c);
18     console.log("b="+b);
19     c=--b;
20     console.log("c="+c);
21     console.log("b="+b);
22     </script>
23 </body>
24 </html>

Guess you like

Origin www.cnblogs.com/nyw1983/p/11510012.html