What are the various ways to create objects in JavaScript?

Answer

Objects in JavaScript can be created in several ways:

  1. Object Literals:
const obj = { key: 'value' };
  1. Object.create():
const obj = Object.create(proto);
  1. Constructor Functions:
function Obj() {
  this.key = 'value';
}
const obj = new Obj();
  1. ES6 Classes:
class Obj {
  constructor() {
    this.key = 'value';
  }
}
const obj = new Obj();
  1. Using new Object():
const obj = new Object();
obj.key = 'value';
  1. Factory Functions:
function createObj() {
  return { key: 'value' };
}
const obj = createObj();

MDN Web Docs: Working with Objects