4-1.算术运算

自动转换一般发生在运算中
1.算数运算 +,-,*,/,%
在算数运算中,先使用Number方法将两边的值转化为number,再计算
tips:特殊情况+,只要有一边是字符串,则起拼接作用,结果一定为字符串
tips:NaN和任何值计算,结果一定是NaN

  1. <SCript>
  2. /*
  3. 自动转换一般发生在运算中
  4. 1.算数运算 +,-,*,/,%
  5. 在算数运算中,先使用Number方法将两边的值转化为number,再计算
  6. tips:特殊情况+,只要有一边是字符串,则起拼接作用,结果一定为字符串
  7. tips:NaN和任何值计算,结果一定是NaN
  8. */
  9. var a = 10;
  10. var b = "20";
  11. var c = true;
  12. console.log(b-a);
  13. console.log(b-c);
  14. </SCript>

4-2.比较运算

,<,==,!=,===运算 在比较运算中,先将两边的值转换成number在进行比较,最终得到的是Boolean值 tips: =赋值,==相等 === 不仅要值相等,也要数据类型一样

  1. <!--
  2. >,<,==,!=,===运算
  3. -->
  4. <script>
  5. /*
  6. 在比较运算中,先将两边的值转换成number在进行比较,最终得到的是Boolean值
  7. */
  8. console.log(true>0);
  9. console.log("11">10);
  10. console.log("9">10);
  11. /* tips: =赋值,==相等 */
  12. console.log("9"==9);
  13. console.log("6"!=true);
  14. // === 不仅要值相等,也要数据类型一样
  15. console.log("9"===9)
  16. console.log(5===5)
  17. </script>

4-3.逻辑运算

  • && 逻辑与
  • 两边都为true,结果才为true。
  • || 逻辑或
  • 只要有一边为true,结果一定为true

两边都为true,结果也为true

  • ! 逻辑非 取反

    1. <script>
    2. /*
    3. && 逻辑与
    4. 两边都为true,结果才为true。
    5. || 逻辑或
    6. 只要有一边为true,结果一定为true
    7. 两边都为true,结果都为true
    8. ! 逻辑非 取反
    9. */
    10. console.log(10>3 && 12>5)
    11. console.log(10>4 && 4>50)
    12. console.log(10>3 || 4>5)
    13. console.log(!(5>4))
    14. </script>

    4-4.特殊情况

  • 逻辑运算可以参与非Boolean的运算

  • 运算的时候,会将值转换成boolean,但返回值不一定是boolean

    1. <script>
    2. /*
    3. 逻辑运算可以参与非Boolean的运算
    4. 运算的时候,会将值转换成boolean,但返回值不一定是boolean
    5. 两边都为true,返回最后一个条件。遇到false直接返回
    6. */
    7. var a = 3 && 4;
    8. var b = 3 && 0;
    9. var c = 0 && 4;
    10. console.log(a);
    11. console.log(b);
    12. console.log(c);
    13. </script>

    image.png

    4-5.混合交错场景

  • 逻辑与优先级高于逻辑或

    4-6.三目运算

  1. <script>
  2. /* 三目运算 */
  3. var b =(4<3)?"大于":"小于"
  4. console.log(b)
  5. </script>