理解JavaScript Truthy和Falsy [英] Understanding JavaScript Truthy and Falsy

查看:143
本文介绍了理解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?

推荐答案


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

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

否。



var a = 0;

var a = 0;

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

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


var a = 10 == 5;

var a = 10 == 5;

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


var a = 1;

var a = 1;

var a = -1;

var a = -1;

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

Any non-zero number including negative numbers is truthy.

引自 HTML


在JavaScript中, truthy值是在布尔上下文中计算时转换为true的值。所有值都是真实的,除非它们被定义为假的(即,除了 false 0 null undefined NaN )。

JavaScript中的虚假值列表: 来自MDN


  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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆