create empty object in javascript

There are several ways to initialize an empty object in JavaScript:

  1. Using object literal notation:
index.tsx
const emptyObject = {};
24 chars
2 lines
  1. Using the Object constructor:
index.tsx
const emptyObject = new Object();
34 chars
2 lines
  1. Using the Object.create() method:
index.tsx
const emptyObject = Object.create(null);
41 chars
2 lines
  1. Using the bracket notation:
index.tsx
const emptyObject = {};
emptyObject['key'] = 'value';
54 chars
3 lines
  1. Using ES6 shorthand notation:
index.tsx
const emptyObject = {};
const key = 'key';
const value = 'value';

const obj = { [key]: value };
97 chars
6 lines

gistlibby LogSnag