Falsy - MDN Web Docs Glossary: Definitions of Web-related terms 编辑

A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context.

JavaScript uses type conversion to coerce any value to a Boolean in contexts that require it, such as conditionals and loops.

There are 8 falsy values:

falseThe keyword false
0The number zero
-0The number negative zero
0nBigInt, when used as a boolean, follows the same rule as a Number. 0n is falsy.
""Empty string value
nullnull - the absence of any value
undefinedundefined - the primitive value
NaNNaN - not a number

Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot. This slot only exists in document.all and cannot be set using JavaScript.

Examples

Examples of falsy values in JavaScript (which are coerced to false in Boolean contexts, and thus bypass the if block):

if (false)
if (null)
if (undefined)
if (0)
if (-0)
if (0n)
if (NaN)
if ("")

The logical AND operator, &&

If the first object is falsy, it returns that object

false && "dog"
// ↪ false

0 && "dog"
// ↪ 0

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'ToBoolean abstract operation' in that specification.

Learn more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:124 次

字数:3784

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文