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

前端 未结 7 2096
半阙折子戏
半阙折子戏 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:27

    It can be understood like this:

    var a= []; //creates a new empty array
    var a= {}; //creates a new empty object
    

    You can also understand that

    var a = {}; is equivalent to var a= new Object();

    Note:

    You can use Arrays when you are bothered about the order of elements(of same type) in your collection else you can use objects. In objects the order is not guaranteed.

提交回复
热议问题