What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

后端 未结 18 1463
生来不讨喜
生来不讨喜 2020-11-21 12:00

What\'s the real difference between declaring an array like this:

var myArray = new Array();

and

var myArray = [];
<         


        
18条回答
  •  你的背包
    2020-11-21 12:49

    In order to better understand [] and new Array():

    > []
      []
    > new Array()
      []
    > [] == []
      false
    > [] === []
      false
    > new Array() == new Array()
      false
    > new Array() === new Array()
      false
    > typeof ([])
      "object"
    > typeof (new Array())
      "object"
    > [] === new Array()
      false
    > [] == new Array()
      false
    

    The above result is from Google Chrome console on Windows 7.

提交回复
热议问题