See Object initializer in Mozilla docs.

In JavaScript, the common way of storing key-values pairs is an associative array.

If you want more control, use the Map type instead.

Create

Empty

const x = {}

typeof x
// 'object'

Or, this works too.

const x = Object()
// {}

With values

Note use of quotes for a key with a hyphen.

const x = {
  foo: "abc",
  bar: 123
  "fizz-buzz": true
}

Get values

const x = {
  foo: "abc",
}

x.foo
// "abc"

x['foo']
// "abc"

x.missingKey
// undefined

Stringify as JSON:

const x = {
  foo: "abc",
}

// Compressed.
JSON.stringify(x)
// '{"foo":"abc"}'

// Expanded.
JSON.stringify(x, null, 2)
// {
//   "foo": "abc"
// }

Unpack

See also Looping cheatsheet for iterating over an associative array.

Object.keys(x)

Object.values(x)

Object.entries(x)

Modify

const x = {}

x.foo = 123
// OR
x['foo'] = 123

x.foo
// 123

Copy

const x = {abc: 123}

const y = {...x}

y
// {abc: 123}

Merge

const x = {abc: 123}
const y = {def: 456}

const z = {
  foo: true,
  ...x,
  ...y
}

z
// { foo: true, abc: 123, def: 456 }

Delete

delete myObject[myKey]

e.g.

const x = {abc: 123}

delete x['abc']

x
// {}

Keys

Literal key

Here, the variable x is ignored.

const x = 'abc123'
// 'abc123'

const y = {
  x: 'Hello, World!'
}

y
// { x: 'Hello, World!' }

Dynamic key

Here, the variable x is used for the key.

const x = 'abc123'
// 'abc123'

const y = {
  [x]: 'Hello, World!'
}
y
// { abc123: 'Hello, World!' }

Allowed

Note that only strings are allowed for keys and other types will be converted to strings.

{ 1: 'Hello, World!' }
// { '1': 'Hello, World!' }

If you want other types, use a Map instead