4-1.算术运算
自动转换一般发生在运算中
1.算数运算 +,-,*,/,%
在算数运算中,先使用Number方法将两边的值转化为number,再计算
tips:特殊情况+,只要有一边是字符串,则起拼接作用,结果一定为字符串
tips:NaN和任何值计算,结果一定是NaN
<SCript>/*自动转换一般发生在运算中1.算数运算 +,-,*,/,%在算数运算中,先使用Number方法将两边的值转化为number,再计算tips:特殊情况+,只要有一边是字符串,则起拼接作用,结果一定为字符串tips:NaN和任何值计算,结果一定是NaN*/var a = 10;var b = "20";var c = true;console.log(b-a);console.log(b-c);</SCript>
4-2.比较运算
,<,==,!=,===运算 在比较运算中,先将两边的值转换成number在进行比较,最终得到的是Boolean值 tips: =赋值,==相等 === 不仅要值相等,也要数据类型一样
<!-->,<,==,!=,===运算--><script>/*在比较运算中,先将两边的值转换成number在进行比较,最终得到的是Boolean值*/console.log(true>0);console.log("11">10);console.log("9">10);/* tips: =赋值,==相等 */console.log("9"==9);console.log("6"!=true);// === 不仅要值相等,也要数据类型一样console.log("9"===9)console.log(5===5)</script>
4-3.逻辑运算
- && 逻辑与
- 两边都为true,结果才为true。
- || 逻辑或
- 只要有一边为true,结果一定为true
两边都为true,结果也为true
! 逻辑非 取反
<script>/*&& 逻辑与两边都为true,结果才为true。|| 逻辑或只要有一边为true,结果一定为true两边都为true,结果都为true! 逻辑非 取反*/console.log(10>3 && 12>5)console.log(10>4 && 4>50)console.log(10>3 || 4>5)console.log(!(5>4))</script>
4-4.特殊情况
逻辑运算可以参与非Boolean的运算
运算的时候,会将值转换成boolean,但返回值不一定是boolean
<script>/*逻辑运算可以参与非Boolean的运算运算的时候,会将值转换成boolean,但返回值不一定是boolean两边都为true,返回最后一个条件。遇到false直接返回*/var a = 3 && 4;var b = 3 && 0;var c = 0 && 4;console.log(a);console.log(b);console.log(c);</script>
4-5.混合交错场景
-
4-6.三目运算
<script>/* 三目运算 */var b =(4<3)?"大于":"小于"console.log(b)</script>
