【发布时间】:2018-12-18 05:21:18
【问题描述】:
在面试的时候我很困惑
- if({}) console.log("hello")
- if([]) console.log("hello")
- if(' ') console.log("hello")
- if('') console.log("hello")
我知道前三个会打印 hello ,但我不知道评估的依据是什么。
甚至我也想知道一些网站或 URL,我可以从中获得这些棘手的问题和解决方案
【问题讨论】:
-
搜索"JavaScript truthy value" - 在
if (expr) stmt;中,当且仅当expr评估为这样的“真实值”时才会执行stmt。