array.length与array.length> 0 [英] array.length vs. array.length > 0

查看:198
本文介绍了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){...}

这篇关于array.length与array.length> 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆