50

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.

5
  • 1
    new keyword in JS Commented Apr 29, 2011 at 1: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 w3schools.com/jsref/jsref_obj_array.asp while the one without new will not. Commented Apr 29, 2011 at 1:49
  • @Flipper I iterated through the properties of each and they both seem to have the same methods. Commented Apr 29, 2011 at 1: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! Commented Apr 29, 2011 at 1:51
  • It looks like calling Array() as a function also returns an array object, just like calling new Array() Commented Apr 29, 2011 at 1:51

4 Answers 4

56

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).

Sign up to request clarification or add additional context in comments.

1 Comment

That sounds solid answer but in reality it is a spec. It can't say how V8 engine or other javascript runtimes out there works. So judging by this answer we can only assume but can't be sure.
9

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."

Comments

5

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.

1 Comment

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.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.