JS数组a ['1']没有给出错误 [英] JS Array a['1'] doesnot gives an error
本文介绍了JS数组a ['1']没有给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经声明了一个数组 a = [1,2,3,4,5]
I have declared an array a = [1,2,3,4,5]
当我写 a [1]
时,它返回2 很好,但是当我写 a ['1']
时,它也给我2 而不是给出错误.
When I write a[1]
it returns 2 which is perfectly fine but when I write a['1']
it also gives me 2 instead of giving an error.
我在那儿期待一个错误.为什么会这样呢?
I was expecting an error there. Why does it behave like that??
推荐答案
所有属性名称都是字符串.
All property names are strings.
如果传递数字,则该数字将转换为字符串,然后再用于查找属性值.
If you pass a number, it gets converted to a string before being used to look up the property value.
console.log(1 == '1');
这篇关于JS数组a ['1']没有给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文