本文介绍了理解JavaScript Truthy和Falsy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以使用以下示例数据解释JavaScript Truthy和Falsy。我已阅读其他主题但仍感到困惑。

Can someone please explain JavaScript Truthy and Falsy, using the below sample data. I have read other threads but still confused.

var a = 0;

var a = 10 == 5;

var a = 1; 

var a = -1;

据我了解,我相信 var a = 1; 是唯一的truthy,其余的都是假的 - 这是正确的吗?

From my understanding, I believe that var a = 1; is the only truthy and the rest are falsy - is this correct?

推荐答案

否。


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

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

这与 var a =(10 == 5)相同); ,所以这是 falsy

var a = -1;

var a = -1;

任何非零包含负数的数字是 truthy

Any non-zero number including negative numbers is truthy.

引自

JavaScript中的虚假值列表:


  1. false

  2. null

  3. undefined

  4. 0

  5. NaN

  6. ''

  7. document.all

  1. false
  2. null
  3. undefined
  4. 0
  5. NaN
  6. ''
  7. document.all

这篇关于理解JavaScript Truthy和Falsy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 12:26