一、自增(++)
⑴什么是自增?
通過自增運算符可以使變量在自身的基礎上加一;
對於一個變量自增以后,原變量的值會立即自增一;
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自增</title> <script> var a = 1; //使a自增1 a++; console.log("a = " +a); </script> </head> <body> </body> </html>
結果:
⑵分類
1、后++(a++)
2、前++(++a)
⑶兩者的相同點
無論是a++,還是++a,都會立即使原變量的值自增1。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自增</title> <script> var a = 1; //使a自增1 // a++; ++a; console.log("a = " +a); </script> </head> <body> </body> </html>
結果:
⑷兩者的不同點
a++和++a的值不同。
①a++的值等於原變量的值(自增前的值)。
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>a++</title> <script> var a = 1; console.log(a++); console.log("a = " +a); </script> </head> <body> </body> </html>
結果:
②++a的值等於原變量自增后的新值。
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>++a</title> <script> var a = 1; console.log(++a); console.log("a = " +a); </script> </head> <body> </body> </html>
結果:
⑸不同點的小測驗
第一個:a++測驗
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>a++</title> <script> var a = 10; a++; console.log(a++); console.log("a = " +a); </script> </head> <body> </body> </html>
結果:
解析:因為初始值a=10,經過a++,變成了11,在console.log(a++)中,a又進行自增1,所以a為12,a++為第一次自增的值,11.
第二個:++a測驗
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>++a</title> <script> var b = 20; console.log(++b); console.log(++b); console.log("b = " +b); </script> </head> <body> </body> </html>
結果:
⑹綜合測驗
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>綜合測驗</title> <script> var d = 20; var result = d++ + ++d + d; console.log("result = " +result); </script> </head> <body> </body> </html>
結果:
解析:首先d++為原值,也就是20;接着d++中的d自增一,變成了21,所以++d為22;++d中的d是由21+1得來的,也就是22.
所以:20+22+22=64.
二、自減(- -)
⑴什么是自減?
通過自減可以使變量在自身的基礎上減一;
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自減</title> <script> var num = 10; num--; console.log("num = " +num); </script> </head> <body> </body> </html>
結果:
⑵分類
1、后- -(a--)
2、前- -(--a)
⑶兩者的相同點
無論是a-- ,還是--a ,都會立即使原變量的值自減一;
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自減</title> <script> var num = 10; --num; console.log("num = " +num); </script> </head> <body> </body> </html>
結果:
⑷兩者的不同點
a-- 和 --a的值不同。
①a- -是變量的原值(是自減前的值);
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>a--</title> <script> var num = 10; console.log(num--); console.log("num = " +num); </script> </head> <body> </body> </html>
結果:
②--a是原變量的新值(自減后的值);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>--a</title> <script> var num = 10; console.log(--num); console.log("num = " +num); </script> </head> <body> </body> </html>
結果:
三、綜合小測試
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>綜合測試</title> <script> var n1 = 10,n2 = 20; var n = n1++; console.log('n1 = ' +n1); n = ++n1; console.log('n = ' +n); console.log('n1 = ' +n1); n = n2--; console.log('n = ' +n); console.log('n2 = ' +n2); n = --n2; console.log('n = ' +n); console.log('n2 = ' +n2); </script> </head> <body> </body> </html>
結果:
怎么樣,有沒有算對呀,看完之后,就應該很明白自增和自減了吧,加油呀~~