array.length与array.length> 0
问题描述:
检查数组的长度是否为真实值与检查数组的长度是否大于0之间有什么区别?
Is there any difference between checking an array's length as a truthy value vs checking that it's > 0?
换句话说,有任何理由要使用其中一项而不是另一项:
In other words is there any reason to use one of these statements over the other:
var arr = [1,2,3];
if (arr.length) {
}
if (arr.length > 0) {
}
答
array.length
最快,并且比array.length > 0
短.您可以看到它们的速度差异: http://jsperf.com/test-of-array-长度
array.length
is fastest and shorter than array.length > 0
. You can see difference of their speeds : http://jsperf.com/test-of-array-length
if(array.length){...}
与if(0){...}
或if(false){...}