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

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

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

var myArray = new Array();

and

var myArray = [];
<         


        
18条回答
  •  -上瘾入骨i
    2020-11-21 12:55

    The first one is the default object constructor call.mostly used for dynamic values.

    var array = new Array(length); //initialize with default length
    

    the second array is used when creating static values

    var array = [red, green, blue, yellow, white]; // this array will contain values.
    

提交回复
热议问题