理解 JavaScript 的真假
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35642809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding JavaScript Truthy and Falsy
提问by tonyf
Can someone please explain JavaScript Truthy and Falsy, using the below sample data. I have read other threads but still confused.
有人可以使用以下示例数据解释 JavaScript Truthy 和 Falsy。我已经阅读了其他线程,但仍然感到困惑。
var a = 0;
var a = 10 == 5;
var a = 1;
var a = -1;
From my understanding, I believe that var a = 1;
is the only truthy and the rest are falsy - is this correct?
根据我的理解,我相信这var a = 1;
是唯一的真理,其余的都是假的 - 这是正确的吗?
回答by Tushar
From my understanding, I believe that var a = 1; is the only truthy and the rest are falsy's - is this correct?
根据我的理解,我认为 var a = 1; 是唯一的真的,其余的都是假的 - 这是正确的吗?
No.
不。
var a = 0;
Number zero is falsy. However, note that the string zero
"0"
is truthy.var a = 10 == 5;
This is same as
var a = (10 == 5);
, so this is falsy.var a = 1;
var a = -1;
Any non-zero number including negative numbers is truthy.
变量 a = 0;
数字零是假的。但是,请注意字符串零
"0"
是真实的。var a = 10 == 5;
这是一样的
var a = (10 == 5);
,所以这是假的。变量 a = 1;
var a = -1;
包括负数在内的任何非零数都是true。
Quoting from MDN
引自MDN
In JavaScript, a truthyvalue is a value that translates to truewhen evaluated in a Boolean context. All values are truthyunless they are defined as falsy(i.e., except for
false
,0
,""
,null
,undefined
, andNaN
).
在 JavaScript 中,真值是在布尔上下文中计算时转换为真的值。所有值均为truthy除非它们被定义为falsy(即,除了
false
,0
,""
,null
,undefined
,和NaN
)。
List of falsy values in JavaScript:From MDN
JavaScript 中的假值列表:来自 MDN
false
null
undefined
0
NaN
''
,""
,``
(Empty template string)document.all
0n
: BigInt-0
false
null
undefined
0
NaN
''
,""
,``
(空模板字符串)document.all
0n
: 大整数-0
回答by Claudiu
There's a simple way to check, which you can use now and forever:
有一种简单的检查方法,您现在和永远都可以使用:
function truthyOrFalsy(a) {
return a ? "truthy" : "falsy";
}
To wit:
以机智:
> truthyOrFalsy(0)
"falsy"
> truthyOrFalsy(10 == 5)
"falsy"
> truthyOrFalsy(1)
"truthy"
> truthyOrFalsy(-1)
"truthy"
回答by bajran
Truthy -> Value that resolve to true in boolean context
Truthy -> 在布尔上下文中解析为 true 的值
Falsy -> Value that resolve to false in boolean context
Falsy -> 在布尔上下文中解析为 false 的值
For better understanding, falsy
values is given below.
为了更好地理解,falsy
下面给出了值。
false
0
empty string
null
undefined
NaN
false
0
empty string
null
undefined
NaN
回答by CharithJ
FALSY
假的
- false
- 0 (zero)
- "", '', `` (empty strings)
- null
- undefined
- NaN (not a number)
- 错误的
- 0(零)
- "", '', ``(空字符串)
- 空值
- 不明确的
- NaN(不是数字)
note : Empty array ([]) is notfalsy
注意:空数组 ([]) 不是假的
TRUTHY
真实
- Everything that is not FALSY
- 一切都不是 FALSY
回答by Jamaluddin Mondal
In JavaScript, &&
and ||
don't always produce a boolean value. Both operators always return the value of one of their operand expressions. Using the double negation !!
or the Boolean
function, "truthy" and "falsy" values can be converted to proper booleans.
在JavaScript中,&&
而||
并不总是产生一个布尔值。两个运算符始终返回其操作数表达式之一的值。使用双重否定!!
或Boolean
函数,可以将“真”和“假”值转换为正确的布尔值。
true && true =>
true
true && false =>
false
true && 'rahul626'=>
"rahul626"
true && 'i am testing Truthy' && ' upvote it'=>
" upvote it"