空数组是假的,但在IF语句返回true [英] Empty array is false, but in IF statement it returns true

查看:225
本文介绍了空数组是假的,但在IF语句返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将返回true:

[] == false

但在这里,警惕被称为:

But here, alert is called:

if([]){ alert('empty array is true here'); }

你能解释一下为什么吗?

Can you explain why?

推荐答案

这是因为 == 在JS力量转换,如果一种类型可以被转换为另一种回报值为真正键,在这里,因为 [] 不能被改变或布尔相比,那是假的搜索结果其中,为如果([])检查和未定义价值观和因为[]既不是null或undefined它返回true

this is because == in JS forces conversion and if one type can be converted to another the return value is true and here because [] cant be changed or compared with bool it is false

where as if([]) checks for null and undefined values and because [] is neither null or undefined it is returning true

检查此
<一href=\"http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use\">JavaScript === VS ==:不要紧,这&QUOT;等于&QUOT;运营商怎么用?

这篇关于空数组是假的,但在IF语句返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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