You can do it both ways. I prefer creating the empty object using {} and then adding the needed props but you can make it by defining the props with the initialization of the value:

 var car = {};

or

var car = { 
    color: null,
    seating: null,
    fuelconsumption: null
};

Just like you did. I dont think there is a best practise for doing this. But maybe the values shoud point the needed type of the data saved this property.

Example:

var car = { 
    color:"",
    seating: "",
    fuelconsumption: ""
};

In you case "" is fine.

If using number NaN, undefined.

If using strings "".

If using objects or arrays {} [].

If using some kind of boolen values true/false

Answer from Iliyan Yotov on Stack Overflow
Top answer
1 of 2
9

You can do it both ways. I prefer creating the empty object using {} and then adding the needed props but you can make it by defining the props with the initialization of the value:

 var car = {};

or

var car = { 
    color: null,
    seating: null,
    fuelconsumption: null
};

Just like you did. I dont think there is a best practise for doing this. But maybe the values shoud point the needed type of the data saved this property.

Example:

var car = { 
    color:"",
    seating: "",
    fuelconsumption: ""
};

In you case "" is fine.

If using number NaN, undefined.

If using strings "".

If using objects or arrays {} [].

If using some kind of boolen values true/false

2 of 2
7

In Javascript, there is often not a need to initialize a property to null. This is because referencing a property that has not been initialized just harmlessly returns undefined which can work just as well as null. This is obviously different than some other languages (Java, C++) where properties of an object must be declared before you can reference them in any way.

What I would think might make sense in your case is to create a constructor function for creating a car object because you will presumably be creating more than one car and all possible car objects won't be known at the time you write the code. So, you create a constructor function like this:

function Car(color, seating, fuel) {
    this.color = color;
    this.seating = seating;
    this.fuelConsumption = fuel;
}

Then, you can create a new Car object like this:

var c = new Car('black', 'leather', 'moderate');
console.log(c.color);   // 'black'
Top answer
1 of 10
523

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.
2 of 10
108

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.

🌐
Medium
easierly.medium.com › creating-empty-object-in-javascript-8ec88fbb917f
Creating empty object in JavaScript… - Medium
May 25, 2022 - Every time you create an object in JavaScript either with the object literal ({}) or using “new Object()” behind the scene JavaScript invokes the constructor function of Object to create the object. It always allows your new object to inherit some properties using the inheritance Object.prototype. Consider the following: ... But sometimes we need to create an object that doesn’t inherit any properties from the prototype. For instance, If you’d like to use an object as a hash/map of arbitrary keys to values.
🌐
Reddit
reddit.com › r/learnjavascript › how to initialise an empty object?
r/learnjavascript on Reddit: How to initialise an empty object?
December 15, 2020 -

I have following scenario. I have an array of objects. I loop through the array and based on value of one object i want to initialise an object. Whats the best approach to do this.

Note: I initialised the object with empty string and it works but I wanted to know if there is any better approach.

🌐
Reddit
reddit.com › r › learnjavascript › comments › 10xwceg › how_to_create_empty_objects_whose_keys_match
How to create empty objects whose keys match given ...
This subreddit is for anyone who wants to learn JavaScript or help others do so. Questions and posts about frontend development in general are welcome, as are all posts pertaining to JavaScript on the backend.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › objects: the basics
Objects
An object can be created with curly braces {…} with an optional list of properties. A property is a “key: value” pair, where key is a string (also called a “property name”), and value can be anything. We can imagine an object as a cabinet with signed files. Every piece of data is stored in its file by the key. It’s easy to find a file by its name or add/remove a file. An empty object (“empty cabinet”) can be created using one of two syntaxes:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Object_initializer
Object initializer - JavaScript - MDN Web Docs - Mozilla
An empty object with no properties can be created like this: ... However, the advantage of the literal or initializer notation is, that you are able to quickly create objects with properties inside the curly braces. You notate a list of key: value pairs delimited by commas.
🌐
EyeHunts
tutorial.eyehunts.com › home › create empty object javascript | basics
Create empty object JavaScript | Basics - Tutorial - By EyeHunts
August 19, 2022 - You can do it both ways. I prefer creating the empty object using {} and then adding the needed props but you can make it by defining the props with the initialization of the value: There initialize a property to null.
Find elsewhere
🌐
Adripofjavascript
adripofjavascript.com › blog › drips › creating-objects-without-prototypes.html
Creating Objects Without Prototypes - A Drip of JavaScript
Every time you create a new object ... scenes JavaScript invokes the Object constructor to create the object, just as if you'd used new Object(). This is what allows your new object to inherit properties from Object.prototype. But sometimes it would be convenient to create an object that doesn't inherit from a prototype at all. For instance, if you'd like to use an object as a hash/map of arbitrary keys to ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-create-an-object-from-the-given-key-value-pairs-using-javascript
How to create an object from the given key-value pairs using JavaScript ? | GeeksforGeeks
August 12, 2024 - We'll discuss various common approaches to achieve this goal, empowering you with the knowledge to handle key-value data effectively in your JavaScript projects. Let's see and discover these approaches together. Following are some approaches to achieve the mentioned target. ... We will explore all the above methods along with their basic implementation with the help of examples. In this approach initializes an empty object and adds key-value pairs using numerical keys, resulting in an object with properties assigned to respective values.
🌐
Quora
quora.com › What-is-the-method-for-creating-an-empty-object-in-JavaScript-without-inheriting-from-an-existing-object
What is the method for creating an empty object in JavaScript without inheriting from an existing object? - Quora
Answer: There are a lot of ways to do this. I’m going to give you just a few and I’ll try to explain when/ why you might want these. Literal Constructor The absolute easiest way to do this is by simply assigning an object to a variable. This is called the declarative form, or a “literal ...
🌐
Mercury
mercury.com › blog › creating-an-emptyobject-type-in-typescript
Creating an EmptyObject type in TypeScript | Mercury
September 19, 2023 - The only way to avoid that is a relatively new compiler flag, --exactOptionalPropertyTypes, which disallows assigning undefined to optional keys. However, enabling this flag in a large codebase could prove difficult or impractical. With this type, all of the previous examples will produce errors now, like we wanted them to: const x: EmptyObject = {foo: 'bar'} // Error 🎉 class FetchTeamMembers extends BackendApiRequest< EmptyObject, // "I have no request body!"
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › create
Object.create() - JavaScript - MDN Web Docs
o = {}; // Is equivalent to: o ... a single property 'p', with value 42. o = Object.create({}, { p: { value: 42 } }); With Object.create(), we can create an object with null as prototype....
🌐
Sprintchase
sprintchase.com › home › creating an empty object in javascript
Creating an Empty Object in JavaScript
June 5, 2025 - Learn how to create an Empty Object in JavaScript using Object literal ({ }) or Object.create(null) method.
🌐
Facebook
facebook.com › groups › javascript.morioh › posts › 2821259944739442
How to create an empty object in Javascript?
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment
🌐
W3Schools
w3schools.com › js › tryit.asp
Creating JavaScript Objects
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
Reddit
reddit.com › r/learnprogramming › how to initialise an empty object in js?
r/learnprogramming on Reddit: How to initialise an empty object in js?
December 15, 2020 -

I have an array of objects. Id of all objects is unique. I am looping through the array and based on id assigning one object from an array to an empty object. So how can i initialise the object its assignment in loop ?

Note: I have initialised it with an string and it works, but since its an object not a string, I was wondering what would be the best approach?

🌐
SamanthaMing
samanthaming.com › tidbits › 94-how-to-check-if-object-is-empty
How to Check if Object is Empty in JavaScript | SamanthaMing.com
const empty = {}; Object.keys(empty).length === 0 && empty.constructor === Object; You may be wondering why do we need the constructor check. Well, it's to cover for the wrapper instances.
🌐
Quora
quora.com › How-do-I-create-a-var-of-multiple-propertys-initially-empty-values-in-JavaScript
How to create a var of multiple property's initially empty values in JavaScript - Quora
Answer (1 of 5): The other answers here already cover you pretty well. I'd just like to add a couple of thoughts/clarifications: So we don't have structs in JS, but we do have object literals. You've already seen this in the answers, but to reiterate: [code js] var myStruct = { value1: "som...