Truthy - MDN Web Docs Glossary: Definitions of Web-related terms 编辑
In JavaScript, a truthy value is a value that is considered true
when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false
, 0
, -0
, 0n
, ""
, null
, undefined
, and NaN
).
JavaScript uses type coercion in Boolean contexts.
Examples of truthy values in JavaScript (which will be coerced to true in boolean contexts, and thus execute the if
block):
if (true)
if ({})
if ([])
if (42)
if ("0")
if ("false")
if (new Date())
if (-42)
if (12n)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of ' ToBoolean abstract operation' in that specification. |
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论