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

后端 未结 18 1460
生来不讨喜
生来不讨喜 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:34

    The first one is the default object constructor call. You can use it's parameters if you want.

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

    The second one gives you the ability to create not empty array:

    var array = [1, 2, 3]; // this array will contain numbers 1, 2, 3.
    

提交回复
热议问题