Create an empty object in JavaScript with {} or new Object()?

Create an empty object in JavaScript with {} or new Object()?

There are two different ways to create an empty object in JavaScript:
var objectA = {}
var objectB = new Object()

Is there any difference in how the script engine handles them? Is there any reason to use one over the other?
Similarly it is also possible to create an empty array using different syntax:
var arrayA = []
var arrayB = new Array()

Solutions/Answers:

Solution 1:

Objects

There is no benefit to using new Object(); — whereas {}; can make your code more compact, and more readable.

For defining empty objects they’re technically the same. The {} syntax is shorter, neater (less Java-ish), and allows you to instantly populate the object inline – like so:

var myObject = {
        title:  'Frog',
        url:    '/img/picture.jpg',
        width:  300,
        height: 200
      };

Arrays

For arrays, there’s similarly almost no benefit to ever using new Array(); over []; — with one minor exception:

var emptyArray = new Array(100);

creates a 100 item long array with all slots containing undefined — which may be nice/useful in certain situations (such as (new Array(9)).join('Na-Na ') + 'Batman!').

My recommendation

  1. Never use new Object(); — it’s clunkier than ‘{}’ and looks silly.
  2. Always use []; — except when you need to quickly create an “empty” array with a predefined length.

Solution 2:

Yes, There is a difference, they’re not the same. It’s true that you’ll get the same results but the engine works in a different way for both of them. One of them is an object literal, and the other one is a constructor, two different ways of creating an object in javascript.

var objectA = {} //This is an object literal

var objectB = new Object() //This is the object constructor

In JS everything is an object, but you should be aware about the following thing with new Object(): It can receive a parameter, and depending on that parameter, it will create a string, a number, or just an empty object.

For example: new Object(1), will return a Number. new Object("hello") will return a string, it means that the object constructor can delegate -depending on the parameter- the object creation to other constructors like string, number, etc… It’s highly important to keep this in mind when you’re managing dynamic data to create objects..

Many authors recommend not to use the object constructor when you can use a certain literal notation instead, where you will be sure that what you’re creating is what you’re expecting to have in your code.

I suggest you to do a further reading on the differences between literal notation and constructors on javascript to find more details.

Solution 3:

These have the same end result, but I would simply add that using the literal syntax can help one become accustomed to the syntax of JSON (a string-ified subset of JavaScript literal object syntax), so it might be a good practice to get into.

One other thing: you might have subtle errors if you forget to use the new operator. So, using literals will help you avoid that problem.

Ultimately, it will depend on the situation as well as preference.

Solution 4:

The object and array literal syntax {}/[] was introduced in JavaScript 1.2, so is not available (and will produce a syntax error) in versions of Netscape Navigator prior to 4.0.

My fingers still default to saying new Array(), but I am a very old man. Thankfully Netscape 3 is not a browser many people ever have to consider today…

Solution 5:

var objectA = {}

is a lot quicker and, in my experience, more commonly used, so it’s probably best to adopt the ‘standard’ and save some typing.

Solution 6:

I believe {} was recommended in one of the Javascript vids on here as a good coding convention. new is necessary for pseudoclassical inheritance. the var obj = {}; way helps to remind you that this is not a classical object oriented language but a prototypal one. Thus the only time you would really need new is when you are using constructors functions. For example:

var Mammal = function (name) {
  this.name = name;
};

Mammal.prototype.get_name = function () {
  return this.name;
}

Mammal.prototype.says = function() {
  return this.saying || '';
}

Then it is used like so:

var aMammal = new Mammal('Me warm-blooded');
var name = aMammal.get_name();

Another advantage to using {} as oppose to new Object is you can use it to do JSON-style object literals.