You're testing different things here.(您在这里测试不同的东西。)
if (arr)
called on object (Array is instance of Object in JS) will check if the object is present, and returns true/false.(if (arr)
在对象(数组是JS中Object的实例if (arr)
调用的if (arr)
将检查该对象是否存在,并返回true / false。)
When you call if (arr == false)
you compare values of this object and the primitive false
value.(调用if (arr == false)
,将比较此对象的值和原始false
值。) Internally, arr.toString()
is called, which returns an empty string ""
.(在内部,将调用arr.toString()
,它返回一个空字符串""
。)
This is because toString
called on Array returns Array.join()
, and empty string is one of falsy values in JavaScript.(这是因为在Array上调用的toString
返回Array.join()
,而空字符串是JavaScript中虚假的值之一。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…