1.9 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			1.9 KiB
		
	
	
	
	
	
	
	
title, localeTitle
| title | localeTitle | 
|---|---|
| Falsy Values | 虚假的价值观 | 
描述
虚假值是评估为FALSE的值,例如在检查变量时。 JavaScript中只有六个falsey值: undefined , null , NaN , 0 , "" (空字符串),当然还有false 。
检查变量上的假值
可以使用简单的条件检查变量中的假值:
if (!variable) { 
  // When the variable has a falsy value the condition is true. 
 } 
一般例子
const string = ""; // <-- falsy
const filledString = "some string in here"; // <-- truthy
const zero = 0; // <-- falsy
let numberGreaterThanZero; // <-- falsy
const emptyArray = []; // <-- truthy, we'll explore more about this next
const emptyObject = {}; // <-- truthy
有阵列的乐趣
if ([] == false) // <-- truthy, will run code in if-block 
 
 if ([]) // <-- truthy, will also run code in if-block 
 
 if ([] == true) // <-- falsy, will NOT run code in if-block 
 
 if (![]) // <-- falsy, will also NOT run code in if-block 
警告
在布尔上下文中评估值时,请注意数据类型。如果值的数据类型是一个_数字_ ,那么truthy / falsy评估可能会导致意外的结果:
const match = { teamA: 0, teamB: 1 } 
 if (match.teamA) 
  // The following won't run due to the falsy evaluation 
  console.log('Team A: ' + match.teamA); 
 } 
上面用例的替代方法是使用typeof评估值:
const match = { teamA: 0, teamB: 1 } 
 if (typeof match.teamA === 'number') 
  console.log('Team A: ' + match.teamA); 
 }