1

What is the difference between these two way of initializing an array in modern javascript environment (in ES6 spec) ?

const arr1 = new Array(3);
const arr2 = Array(3);

They look same running in Chrome console, and arr2 is also the instance of Array.

arr2 instanceof Array;
// true
Megan610
  • 11
  • 3
  • 1
    Use the array literal syntax `[]`... oh you weren't asking for another option. – evolutionxbox Dec 05 '17 at 10:08
  • see this [https://stackoverflow.com/questions/8205691/coffeescript-array-vs-new-array](https://stackoverflow.com/questions/8205691/coffeescript-array-vs-new-array) – vibhor1997a Dec 05 '17 at 10:10
  • 1
    _"Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments."_ --- From the ES2015 specs. – evolutionxbox Dec 05 '17 at 10:13

0 Answers0