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:
false | The keyword false |
0 | The number zero |
-0 | The number negative zero |
0n | BigInt, when used as a boolean, follows the same rule as a Number. 0n is falsy. |
"" | Empty string value |
null | null - the absence of any value |
undefined | undefined - the primitive value |
NaN | NaN - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论