理解 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:04:00  来源:igfitidea点击:

Understanding JavaScript Truthy and Falsy

javascript

提问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.

不。

  1. var a = 0;

    Number zero is falsy. However, note that the string zero "0"is truthy.

  2. var a = 10 == 5;

    This is same as var a = (10 == 5);, so this is falsy.

  3. var a = 1;

    var a = -1;

    Any non-zero number including negative numbers is truthy.

  1. 变量 a = 0;

    数字零是假的。但是,请注意字符串零"0"是真实的。

  2. var a = 10 == 5;

    这是一样的var a = (10 == 5);,所以这是假的

  3. 变量 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, and NaN).

在 JavaScript 中,值是在布尔上下文中计算时转换为真的值。所有值均为truthy除非它们被定义为falsy(即,除了false0""nullundefined,和NaN)。

List of falsy values in JavaScript:From MDN

JavaScript 中的假值列表:来自 MDN

  1. false
  2. null
  3. undefined
  4. 0
  5. NaN
  6. '', "", ``(Empty template string)
  7. document.all
  8. 0n: BigInt
  9. -0
  1. false
  2. null
  3. undefined
  4. 0
  5. NaN
  6. '', "", ``(空模板字符串)
  7. document.all
  8. 0n: 大整数
  9. -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"

Also see a list of all falsey values in JavaScript.

另请参阅JavaScript 中所有 falsey 值的列表

回答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, falsyvalues is given below.

为了更好地理解,falsy下面给出了值。

  1. false
  2. 0
  3. empty string
  4. null
  5. undefined
  6. NaN
  1. false
  2. 0
  3. empty string
  4. null
  5. undefined
  6. 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 Booleanfunction, "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"

Try, tried on console

试试,在控制台上试过

Well described here

在这里描述得很好