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...
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()
10 Answers
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
};