if(xx) a==b 运算符常见误区

发布时间:2026-05-07 21:02

巧用缩写和符号节省空间:如A/B/C代表主要观点。 #生活技巧# #学习技巧# #笔记技巧#

最新推荐文章于 2024-07-23 21:00:06 发布

转载 于 2018-01-07 22:49:00 发布 · 293 阅读

· 0

· 0 ·

CC 4.0 BY-SA版权

If(xx)

1 // 题目1:如下代码输出什么? 2 if ("hello") { 3 console.log("hello") 4 } //true 5 6 // 题目2:如下代码输出什么? 7 if ("") { 8 console.log('empty') 9 }//false 10 11 // 题目3:如下代码输出什么? 12 if (" ") { 13 console.log('blank') 14 }//true 15 // 题目4:如下代码输出什么? 16 if ([0]) { 17 console.log('array') 18 }//true 19 20 if('0.00'){ 21 console.log('0.00') 22 }//true 23 24 if(+0.00){ 25 console.log('haha') 26 }//false +转换为数字 27 28 if([]){ 29 console.log('haha') 30 }//turn

括号里的表达式,会强制转换为布尔类型。 不要写上面误导的写法,if(a === 0){ }   

类型结果UndefinedfalseNullfalseBoolean直接判断Number+0, −0, 或者 NaN 为 false, 其他为 trueString空字符串为 false,其他都为 trueObjecttrue

==的判断

1 "" == 0 //题目1 2 " " == 0 //题目2 3 "" == true //题目3 4 "" == false //题目4 5 " " == true //题目5! 6 " " == true //题目6 7 !" " == false //题目7 8 "hello" == true //题目8 9 "hello" == false //题目9 10 "0" == true //题目10 11 "0" == false //题目11 12 "00" == false //题目12 13 "0.00" == false //题目13 14 undefined == null //题目14 15 true == true //题目15 16 [] == true //题目16 17 var obj = { 18 a: 0, 19 valueOf: function(){return 1} 20 } 21 obj == "[object Object]" //题目17 22 obj == 1 //题目18 23 obj == true //题目19

解密

xy结果nullundefinedtrueNumberStringx == toNumber(y)Boolean(any)toNumber(x) == yObjectString or NumbertoPrimitive(x) == yotherwiseotherwisefalsetoNumber typeResultUndefinedNaNNull0Booleanture -> 1, false -> 0String“abc” -> NaN, “123” -> 123toPrimitive

对于 Object 类型,先尝试调用 .valueOf 方法获取结果。 如果没定义,再尝试调用 .toString方法获取结果

转载于:https://www.cnblogs.com/zhangzheng022/p/8232713.html

网址:if(xx) a==b 运算符常见误区 https://www.yuejiaxmz.com/news/view/1456189

相关内容

python运算符
Python初学(3)运算符
shell 运算符
java 模运算 a%b = a
int a=3,b=2,c=1; if(a>b && b&
python 输出 a+b
定义运算a⊕b=a(1
js 运算符
Python中的不等于运算符详解
c = a**2 + b b = a return c a = 10 b...

随便看看