ithome
参考资料1:MDN — the Mozilla Developer Network
参考资料2:从ES6开始的JavaScript学习生活
参考资料3:Fooish 程序技术
三种逻辑运算符:
&&
: 符号左右都是true
才会得到true
;其中任一方false
,则得到false
。||
:符号左右任一方为true
,得到true
;两方都false
,才会得到false
。!
:原本是true
,经!
转换会变成false
!!(xxx)
可以当Boolean(xxx)
用!!'false' == !!'true' // true 经过!!转换,两个值都是Boolean,且字串总是ture
!!'false' === !!'true' // true 即便使用严格比较===也是true,理由同上
哪些资料类型经ToBoolean
会转换成falsy?
Undefined
Null
0
, -0
, NaN
""
or ''
Boolean("false"); // true
Boolean("0"); // true
Boolean(-0); // false
Boolean("''''"); // true
Boolean(''); // false
const arr = [1, 2, 3, null, 'a', 0]
arr.filter(Boolean) // [1, 2, 3, 'a'] null跟0都是falsy,因此被清掉了
arr // arr = [1, 2, 3, null, 'a', 0]阵列只是复制,没有被改变 (immutable)
短路求值(Short-circuit)
&&
)、(||
)也称为短路求值(Short-circuit)运算符||
)运算时,如果第1个运算子为"falsy"时,回传第2个运算子。否则,回传第1个运算子。&&
)运算时,如果第1个运算子为"falsy"时,回传第1个运算子。否则,直接回传第2个运算子。刚好跟(||
)相反
console.log('foo' || 'bar') // 'foo'
console.log(false || 'bar') // 'bar'
console.log(false && 'bar') // false
console.log( 0 || '' || 5 || 'bar') //5
console.log(false || null || '' || 0 || NaN || 'Hello' || undefined) //'Hello'
let a = value || 5 // 5是预设值
<<: Day 30 Review security tools and features
>>: [Day 30]-【STM32系列终章之不负责任地说再见】
本文将介绍如何使用 JWT 保持登入状态,配合 Swagger / OpenAPI 呈现。 .NET...
前言 Pandas 是强大的资料科学分析工具,结合前几天所学的NumPy特性 提供方便读取及使用的资...
甚麽是资料结构? 资料结构(Data structure) 简单来说,就是一个含有结构的资料型别...
有时候在制作区块时,会希望用不规则的形状呈现,以前会将图片制作成不规则形状,在放到html里面,或是...
yo, what's up? Product Type Product types 允许同时存在两种...