🌐
React
react.dev › learn › updating-arrays-in-state
Updating Arrays in State – React
Here, ... is the object spread syntax used to create a copy of an object.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › create
Object.create() - JavaScript | MDN
The Object.create() static method creates a new object, using an existing object as the prototype of the newly created object.
Discussions

[AskJS] ways to create a javascript object?
Object.create() Object(x) converts values x to objects (if x is an object, it is returned unchanged) Object() creates a new empty object More on reddit.com
🌐 r/javascript
13
0
April 19, 2022
javascript - Is creating JS object with Object.create(null) the same as {}? - Stack Overflow
If you are using an object as a map, and you create an object using method 1 above, then you have to be extra careful when doing lookups in the map. Because the properties and methods from Object are inherited, your code may run into a case where there are keys in the map that you never inserted. More on stackoverflow.com
🌐 stackoverflow.com
Which way is best for creating an object in JavaScript? Is `var` necessary before an object property? - Stack Overflow
So far I saw three ways for creating an object in JavaScript. Which way is best for creating an object and why? I also saw that in all of these examples the keyword var is not used before a proper... More on stackoverflow.com
🌐 stackoverflow.com
Which way is the best way to create objects in Javascript?
EDIT: This post has been published--refined and expanded--as a SitePoint article. https://www.sitepoint.com/javascript-object-creation-patterns-best-practises/ So, here's how things evolved. Simple objects. Obviously the simplest way to make an object in JavaScript is an object literal. var o = { x: 42, y: 3.14, f: function() {}, g: function() {} }; But there's a drawback. If you need to use the same type of object in other places, then you'll end up copy-pasting the object's structure and initialization. The fix... 2) Factory functions. Objects are created, initialized, and returned from functions. function thing() { return { x: 42, y: 3.14, f: function() {}, g: function() {} }; } var o = thing(); But there's a drawback. We're creating fresh copies of functions "f" and "g" with each object. It would be better if all "thing" objects could share just one copy of each function. (Note: JavaScript engines are heavily optimized, so this issue is less important today than it used to be.) The fix... 3) Delegating to prototypes. JavaScript makes it easy to delegate property accesses to other objects through what we call the prototype chain. var thingPrototype = { f: function() {}, g: function() {} }; function thing() { var o = Object.create(thingPrototype); o.x = 42; o.y = 3.14; return o; } var o = thing(); This is such a common pattern that the language has some built-in support. A prototype object is created automatically for each function. thing.prototype.f = function() {}; thing.prototype.g = function() {}; function thing() { var o = Object.create(thing.prototype); o.x = 42; o.y = 3.14; return o; } var o = thing(); But there's a drawback. This is going to result in some repetition. In the "thing" function, the first and last lines are going to be repeated almost verbatim in every such delegating-to-prototype-factory-function. The fix... 4) Consolidate the repetition. function create(func) { var o = Object.create(func.prototype); func.call(o); return o; } Thing.prototype.f = function() {}; Thing.prototype.g = function() {}; function Thing() { this.x = 42; this.y = 3.14; } var o = create(Thing); The repetitive lines from "thing" have been moved into "create" and made generic to operate on any such function. This too is such a common pattern that the language has some built-in support. The "create" function we defined is actually a rudimentary version of the "new" keyword. Thing.prototype.f = function() {}; Thing.prototype.g = function() {}; function Thing() { this.x = 42; this.y = 3.14; } var o = new Thing; We've now arrived at ES5 classes. They are object creation functions that delegate shared properties to a prototype object and rely on the new keyword to handle repetitive logic. But there's a drawback. It's verbose and ugly. And implementing the notion of inheritance is even more verbose and ugly. The fix... 5) ES6 classes. They offer a significantly cleaner syntax for doing the same thing. class Thing { constructor() { this.x = 42; this.y = 3.14; } f() {} g() {} } var o = new Thing; More on reddit.com
🌐 r/javascript
78
249
March 27, 2016
🌐
Code Institute
codeinstitute.net › blog › javascript › creating an object in javascript
How to Create an Object in Javascript - Code Institute Global
April 24, 2023 - The first thing you have to do is create a variable of type Object. Then, you can assign values to that variable. ... For example, this code creates a variable called myObject and assigns it the value of “Hello”. ... There are many ways ...
🌐
W3Schools
w3schools.com › js › js_object_definition.asp
JavaScript Object Definitions
JavaScript defines 7 types of primitive data types: Primitive values are immutable (they are hardcoded and cannot be changed). if x = 3.14, you can change the value of x, but you cannot change the value of 3.14. Objects are mutable: They are addressed by reference, not by value. If person is an object, the following statement will not create ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_objects
Working with objects - JavaScript | MDN
February 21, 2026 - For example, this example creates an object named myCar, with properties named make, model, and year, with their values set to "Ford", "Mustang", and 1969: ... Like JavaScript variables, property names are case sensitive. Property names can only be strings or Symbols — all keys are converted to strings unless they are Symbols.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › objects-in-javascript
Objects in JavaScript - GeeksforGeeks
Using Object Literal Syntax ({}): This is the most common and simple way to create objects. Using the Object Constructor (new Object()): This uses JavaScript's built-in Object constructor to create objects.
Published   January 16, 2026
🌐
Quora
quora.com › How-do-you-create-a-JavaScript-object
How to create a JavaScript object - Quora
Answer: Javascript objects are related to real life world objects.It is a entity which have some properties and respective types. Objects and Arrays are mutable in nature it means it's state can be modified after it is created. For example : Your coffee cup have certain features like it's colou...
🌐
Medium
medium.com › @mandeepkaur1 › creating-objects-in-javascript-a896e6cfa6eb
Creating Objects in JavaScript. JavaScript is an object-based language… | by Mandeep Kaur | Medium
October 16, 2020 - For example, the getName() method ... method is calling. ... The second method of creating a JavaScript object is to use the constructor function....
Top answer
1 of 5
260

They are not equivalent. {}.constructor.prototype === Object.prototype while Object.create(null) doesn't inherit from anything and thus has no properties at all.

In other words: A JavaScript object inherits from Object by default, unless you explicitly create it with null as its prototype, like: Object.create(null).

{} would instead be equivalent to Object.create(Object.prototype).


In Chrome DevTools you can see that Object.create(null) has no prototype, while {} does.

2 of 5
115

They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.

  1. var p = {};

    Creates an object that inherits the properties and methods from Object.

  2. var p2 = Object.create(null);

    Creates an object that doesn't inherit anything.

If you are using an object as a map, and you create an object using method 1 above, then you have to be extra careful when doing lookups in the map. Because the properties and methods from Object are inherited, your code may run into a case where there are keys in the map that you never inserted. For example, if you did a lookup on toString, you would find a function, even though you never put that value there. You can work around that like this:

if (Object.prototype.hasOwnProperty.call(p, 'toString')) {
    // we actually inserted a 'toString' key into p
}

Note that it is fine to assign something to p.toString, it will simply override the inherited toString function on p.

Note that you can't just do p.hasOwnProperty('toString') because you may have inserted a key "hasOwnProperty" into p, so we force it to use the implementation in Object.prototype.

On the other hand, if you use method 2 above, then you won't have to worry about things from Object appearing in the map.

You can't check for the existence of a property with a simple if like this:

// Unreliable:
if (p[someKey]) {
    // ...
}

The value might be an empty string, might be false, or null, or undefined, or 0, or NaN, etc. To check whether a property exists at all, you would still need to use Object.prototype.hasOwnProperty.call(p, someKey).

🌐
Amazon Web Services
aws.amazon.com › products › storage › amazon s3
Amazon S3 - Cloud Object Storage - AWS
3 days ago - By reducing the cost of storing and querying vectors by up to 90% while maintaining subsecond query performance, S3 Vectors makes it cost-effective to create and use large vector datasets to improve the memory and context of AI agents as well as conduct semantic search results of your S3 data. With S3 Vectors, developers can get started quickly, reduce operational complexity, and scale vector-driven semantic search and AI workloads efficiently on a durable, trusted foundation. ... Meet your recovery time objective (RTO), recovery point objective (RPO), and compliance requirements with the robust replication functionality of S3, data protection with AWS Backup, and various AWS Partner Network solutions.
Top answer
1 of 8
186

There is no best way, it depends on your use case.

  • Use way 1 if you want to create several similar objects. In your example, Person (you should start the name with a capital letter) is called the constructor function. This is similar to classes in other OO languages.
  • Use way 2 if you only need one object of a kind (like a singleton). If you want this object to inherit from another one, then you have to use a constructor function though.
  • Use way 3 if you want to initialize properties of the object depending on other properties of it or if you have dynamic property names.

Update: As requested examples for the third way.

Dependent properties:

The following does not work as this does not refer to book. There is no way to initialize a property with values of other properties in a object literal:

var book = {
    price: somePrice * discount,
    pages: 500,
    pricePerPage: this.price / this.pages
};

instead, you could do:

var book = {
    price: somePrice * discount,
    pages: 500
};
book.pricePerPage = book.price / book.pages;
// or book['pricePerPage'] = book.price / book.pages;

Dynamic property names:

If the property name is stored in some variable or created through some expression, then you have to use bracket notation:

var name = 'propertyName';

// the property will be `name`, not `propertyName`
var obj = {
    name: 42
}; 

// same here
obj.name = 42;

// this works, it will set `propertyName`
obj[name] = 42;
2 of 8
118

There is various way to define a function. It is totally based upon your requirement. Below are the few styles :-

  1. Object Constructor
  2. Literal constructor
  3. Function Based
  4. Protoype Based
  5. Function and Prototype Based
  6. Singleton Based

Examples:

  1. Object constructor
var person = new Object();

person.name = "Anand",
person.getName = function(){
  return this.name ; 
};
  1. Literal constructor
var person = { 
  name : "Anand",
  getName : function (){
   return this.name
  } 
} 
  1. function Constructor
function Person(name){
  this.name = name
  this.getName = function(){
    return this.name
  } 
} 
  1. Prototype
function Person(){};

Person.prototype.name = "Anand";
  1. Function/Prototype combination
function Person(name){
  this.name = name;
} 
Person.prototype.getName = function(){
  return this.name
} 
  1. Singleton
var person = new function(){
  this.name = "Anand"
} 

You can try it on console, if you have any confusion.

🌐
Hng
hng.tech › learn › learn-javascript-programming-online-with-step-by-step-video-tutorials › creating-and-using-objects-in-javascript
Creating and using Objects in JavaScript | HNG Learn
Built-In Objects: JavaScript includes ... Creating Custom Objects: Users can create their own objects in JavaScript by defining a variable as an object and assigning values to its properties....
🌐
DEV Community
dev.to › oluwadamisisamuel1 › how-to-create-objects-in-javascript-1l9
How to Create Objects in JavaScript - DEV Community
June 28, 2024 - The simplest way to create an object in JavaScript is by using an object literal.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object
Object - JavaScript | MDN
The Object type represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities. Objects can be created using the Object() constructor or the object initializer / literal syntax.
🌐
BrainStation®
brainstation.io › learn › javascript › object
JavaScript Object (2026 Tutorial & Examples) | BrainStation®
February 4, 2025 - You can create a new object using the keyword new, you can create a single object using an object literal, you can use an object constructor function to create an object type, or you can create an object using Object.create(). Creating JavaScript ...
🌐
Reddit
reddit.com › r/javascript › which way is the best way to create objects in javascript?
r/javascript on Reddit: Which way is the best way to create objects in Javascript?
March 27, 2016 -

Dear all,

I am trying to figure out Javascript syntax for creating objects, and it is confusing me a little bit, because there seem to be multiple ways of doing it.

I've seen:

(1)

function book(title, author){ 
     this.title = title;
     this.author = author;
}

var somebook = new book("Harry Potter", "J.K. Rowling");

I've also seen the new ECMAscript 6 "class" identifier.

I've also seen:

(2)

var book = {
     title: "Harry Potter", author: "J.K. Rowling"
}

Is it recommended to write out a constructor function, like in (1)?

If we simply create a particular object directly, like (2), would subsequent instantiations of book objects work if we give it a different set of properties?

What is the best way to think about objects in Javascript? Thanks.

Top answer
1 of 5
659
EDIT: This post has been published--refined and expanded--as a SitePoint article. https://www.sitepoint.com/javascript-object-creation-patterns-best-practises/ So, here's how things evolved. Simple objects. Obviously the simplest way to make an object in JavaScript is an object literal. var o = { x: 42, y: 3.14, f: function() {}, g: function() {} }; But there's a drawback. If you need to use the same type of object in other places, then you'll end up copy-pasting the object's structure and initialization. The fix... 2) Factory functions. Objects are created, initialized, and returned from functions. function thing() { return { x: 42, y: 3.14, f: function() {}, g: function() {} }; } var o = thing(); But there's a drawback. We're creating fresh copies of functions "f" and "g" with each object. It would be better if all "thing" objects could share just one copy of each function. (Note: JavaScript engines are heavily optimized, so this issue is less important today than it used to be.) The fix... 3) Delegating to prototypes. JavaScript makes it easy to delegate property accesses to other objects through what we call the prototype chain. var thingPrototype = { f: function() {}, g: function() {} }; function thing() { var o = Object.create(thingPrototype); o.x = 42; o.y = 3.14; return o; } var o = thing(); This is such a common pattern that the language has some built-in support. A prototype object is created automatically for each function. thing.prototype.f = function() {}; thing.prototype.g = function() {}; function thing() { var o = Object.create(thing.prototype); o.x = 42; o.y = 3.14; return o; } var o = thing(); But there's a drawback. This is going to result in some repetition. In the "thing" function, the first and last lines are going to be repeated almost verbatim in every such delegating-to-prototype-factory-function. The fix... 4) Consolidate the repetition. function create(func) { var o = Object.create(func.prototype); func.call(o); return o; } Thing.prototype.f = function() {}; Thing.prototype.g = function() {}; function Thing() { this.x = 42; this.y = 3.14; } var o = create(Thing); The repetitive lines from "thing" have been moved into "create" and made generic to operate on any such function. This too is such a common pattern that the language has some built-in support. The "create" function we defined is actually a rudimentary version of the "new" keyword. Thing.prototype.f = function() {}; Thing.prototype.g = function() {}; function Thing() { this.x = 42; this.y = 3.14; } var o = new Thing; We've now arrived at ES5 classes. They are object creation functions that delegate shared properties to a prototype object and rely on the new keyword to handle repetitive logic. But there's a drawback. It's verbose and ugly. And implementing the notion of inheritance is even more verbose and ugly. The fix... 5) ES6 classes. They offer a significantly cleaner syntax for doing the same thing. class Thing { constructor() { this.x = 42; this.y = 3.14; } f() {} g() {} } var o = new Thing;
2 of 5
11
I prefer ES2015 syntax. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes class Polygon { constructor(height, width) { this.height = height; this.width = width; } } Of course, this means you'll need to convert it to current syntax, so you'll have to use something like Babel to make it work on modern browsers. For the record, Babel converts that to this: "use strict"; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Polygon = function Polygon(height, width) { _classCallCheck(this, Polygon); this.height = height; this.width = width; };
Top answer
1 of 4
7

It's really down to taste. This way:

var anotherVariable = new someObjectType({
    property1:    "Some value for this named property",
    property2:    "This is the value for property 2"
});

... is generally better if there's more than 2/3 arguments, as it aids readability and makes it easier to avoid the optional argument problem (fn(null,null,null,123')).

Another consideration is performance. Passing arguments in the conventional way will be faster, but this speed gain only becomes significant in very performance-sensitive situations.

Can I use that second way to instantiate a variable of an object type that has been defined using the "classical"way of defining the object type with that implicit constructor?

Not easily. If you want to instantiate a constructor by using a hash instead of just passing arguments, and you don't have control over the source, then you could "wrap" it:

var _constructor = SomeConstructorFunction;

SomeConstructorFunction = function(hash) {
    return new _constructor(hash.property1, hash.property2);
};

I wouldn't really recommend messing with third-party APIs just for the sake of style though.

If I want to create an array of these objects, are there any other considerations?

How big is the array? What's the array for exactly? Performance might be worth considering...

2 of 4
3

Best way to create javascript objects is quit using new (at least if you subscribe to the crockford camp)

myObjectType = function(props) {
  // anything defined on props will be private due to the closure

  props.privateVar = "private";

  // anything defined on obj will be public
  obj = {};

  obj.testing = new function() {
    alert(props.privateVar);
  };

  return obj;
};
instance = myObjectType({prop1: "prop"});

// if we want inheritance, it just means starting with the base type instead of
// a new object
subType = function(props) {
  obj = myObjectType(props);
  obj.subTypeProperty = "hello.";

  return obj;
};

Page 52 of Javascript: The Good Parts, I highly recommend it :-)