Are There Any Differences Between Using "Array()" and "new Array()" in JavaScript?

There are no differences between calling Array() as a function or as a constructor. According to the spec, using Array(…) as a function is equivalent to using the expression new Array(…) to create an Array object instance with the same arguments. To demonstrate this, let's consider the following examples:

// output: []
console.log(Array());
console.log(new Array());
const x = Array(3);
const y = new Array(3);

// output: array of 3 empty slots
console.log(x);
console.log(y);

// output: 3
console.log(x.length);
console.log(y.length);
const x = Array(1, 2, 3);
const y = new Array(1, 2, 3);

// output: [1, 2, 3]
console.log(x);
console.log(y);

// output: 3
console.log(x.length);
console.log(y.length);

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.