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

前端 未结 7 2148
半阙折子戏
半阙折子戏 2020-12-02 10:04

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

var a=[];

What is the meaning of de

7条回答
  •  天涯浪人
    2020-12-02 10:17

    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);
    

提交回复
热议问题