42

I just started thinking about this, but couldn't get any differences to expose themselves whilst mucking around in jsFiddle.

var a = new Array(1),
    b = Array(1);

console.log(a, b);

Output is two arrays with one undefined member.

Doing a for ( in ) reveals they have the same properties.

What are the differences between these? Does the first one simply instantiate the object explicitly?

Please don't lecture me about using array literal notation as I already know about that. I'm more wishing to fill this gap in my knowledge explained above.

alex
  • 438,662
  • 188
  • 837
  • 957
  • 1
    [new keyword in JS](http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript) – Jonathon Faust Apr 29 '11 at 01:48
  • I am going to take a wild stab at it and say that new means that it is an object like the one without new is not. The one with new will have methods as described here http://www.w3schools.com/jsref/jsref_obj_array.asp while the one without new will not. – Flipper Apr 29 '11 at 01:49
  • @Flipper I iterated through the properties of each and they both seem to have the same methods. – alex Apr 29 '11 at 01:49
  • @alex yeah I did the same thing just now and did not find a difference. There's a reason I only left a comment and not an answer. Upvote on the question though! – Flipper Apr 29 '11 at 01:51
  • It looks like calling Array() as a function also returns an array object, just like calling new Array() – Max Apr 29 '11 at 01:51

4 Answers4

47

With Array, both are equivalent. The new is injected when it's called as a function:

15.4.1 The Array Constructor Called as a Function

When Array is called as a function rather than as a constructor, it creates and initialises a new Array object. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.

From ECMA-262, 3th Edition (with similar in 5th Edition). See also 22.1.1 The Array Constructor in ECMA-262 ECMAScript 2020 specification (11th Edition).

Community
  • 1
  • 1
Jonathan Lonowski
  • 112,514
  • 31
  • 189
  • 193
6

According to Javascript: The Definitive Guide (5th Edition), page 602, "When the Array() constructor is called as a function, without the new operator, it behaves exactly as it does when called with the new operator."

Brigham
  • 13,690
  • 3
  • 34
  • 45
4

The difference lies in the implementation of the Array function. Whether a call to Array without a new operator will return an instance of Array or not is implementation dependent. For example Mozilla's SpiderMonkey engine does this:

static JSBool
Array(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
     jsuint length;
     jsval *vector;

     /* If called without new, replace obj with a new Array object. */

That is an actual comment from the actual source. Next lines of code are not reproduced here. I would suppose other engines do the same. Otherwise the behavior is undefined. A good read on this topic is John Resig's post here.

Satyajit
  • 3,781
  • 1
  • 17
  • 14
  • John Resig's post is about user-defined functions. The behavior of `Array` without `new` is well-defined by the ECMA spec, as described in the other answers. – ZachB May 17 '18 at 01:49
1

new creates a new object (class instance) which is passed to constructor function as this. But some functions detect if they were not called using new and behave the same way as if they were. Array is one of them, so there is no any difference.

If you want to create such constructor yourself you can do it like in following code:

function Smth(val) {
  if (!(this instanceof Smth)) {
    return new Smth(val);
  }

  this.val = val;
}
Qwertiy
  • 14,618
  • 9
  • 41
  • 96