async githubgit
布尔值,JavaScript 基本数据类型之一。github
JavaScript 基本数据类型:async
number 数字this
string 字符串code
boolean 布尔值ip
undefined字符串
nullget
symbolsstring
objectit
值有两个:true 、 false
最多用的是在条件判断(if
, ? :
, while
等),对非布尔值,解释引擎会先进行隐式转换为布尔值。
对于大部分值在转换为布尔值时都是 true
, 如下 falsy 值被转为false
:
空串 ""
null
undefined
数字 0
数字 NaN
布尔值 false
这些值一般由某些操做返回,如:
逻辑类操做, !
、&&
、||
比较类操做,===
、!=
、>
and so on
能够转换为布尔类型的值或变量
经常使用小技巧:
!!
!!
转成 boolean
let stringTest = 'this is a string'; true === !!stringTest // true
一些容易出错的点
null == undefined // true null === undefined // false NaN == NaN // false
有两个小技巧:
||
let stringTest = 'this is a test'; // 已经定义, 保留原有值 let testA = stringTest || 'default'; testA // 'this is a test'
&&
let objectTest = {a1: {a2: 'a2'}}; const a2 = objectTest.a1 && objectTest.a1.a2 || 'default'; a2 // 'a2'
在使用上面方式进行赋值或者条件判断时,务必要考虑 falsy 的状况。以下面的代码可能会成为一个隐患
const warningNumber = 0; // 数字 0 被当成了 false 处理 const notWant = warningNumber || 10; notWant; // 10