What’s the difference between “{}” and “[]” while

2019-01-17 04:26发布

What’s the difference between “{}” and “[]” while declaring a JavaScript array? Normally I declare like

var a=[];

What is the meaning of declaring the array as var a={}

7条回答
倾城 Initia
2楼-- · 2019-01-17 04:54

When you declare

var a=[];

you are declaring a empty array.

But when you are declaring

var a={};

you are declaring a Object .

Although Array is also Object in Javascript but it is numeric key paired values. Which have all the functionality of object but Added some few method of Array like Push,Splice,Length and so on.

So if you want Some values where you need to use numeric keys use Array. else use object. you can Create object like:

var a={name:"abc",age:"14"}; 

And can access values like

console.log(a.name);
查看更多
登录 后发表回答