注:当不清楚运算符的优先级时,给需要优先运算的式子加上()

1.算数运算

  1. 算数运算符+,—,*,/,%
  2. 1.除+运算外,先将两边的值转为number,再计算
  3. 2.+,有一边为字符串,结果一定为字符串
  4. 示例:var a = 10;
  5. var b = 20;
  6. console.log(a+b);
  7. console.log(a-b);
  8. console.log(a*b);
  9. console.log(10/20)

2.比较运算

  1. 在比较运算中,先将两边的值转为number,再计算Number
  2. 返回值是boolean
  3. 示例;var a = 10;
  4. var a = 20;
  5. console.log(a>b);
  6. console.log(a<b);
  7. console.log(a==b);
  8. /* =是赋值语句 ==才是比较运算 */
  9. console.log(a!=b);

3.逻辑运算

  1. &&逻辑与,||逻辑或,!逻辑非
  2. 1.逻辑与
  3. 特点:只有两边都为true,结果才为true,只要有一边为false,结果就为false
  4. console.log(10>5 && 30>20)
  5. console.log(10<5 && 30>20)
  6. 2.逻辑或
  7. 特点:只要有一边为true,结果就为true,如果两边都为falase,结果一定是false
  8. console.log(10>20 || 30>20)
  9. 3.逻辑非
  10. console.log(!(30>40))

4.赋值运算

  1. 赋值运算 +=,-=,*=,/=,%
  2. var a = 10;
  3. a+=20;
  4. // a = a+20;
  5. console.log(a);
  6. var b = 30;
  7. b-=40;
  8. // b=b-40
  9. console.log(b);

5.三元(三目)

  1. 语法:条件表达式?语句1:语句二
  2. 经典三木运算:
  3. 比大小:
  4. var a =30;
  5. var b =140;
  6. var c = 50;
  7. var max = a>b?(a>c?a:c):(b>c?b:c)
  8. /* var value = (code)? block1:block2;
  9. code == true 输出block1
  10. code == false 输出block2
  11. */
  12. var a = (10<5)?"true":"false";
  13. console.log(a)

6.++

  1. ++放在后面,先运算后自增
  2. ++放在前面,先自增后运算
  3. // var b = ++a +20;
  4. // console.log(a); //21
  5. // console.log(b); //41
  6. var b = a++ +20;
  7. console.log(a); //21
  8. console.log(b); //40