JavaScript Object Functions Cheat Sheet
Tags: javascript, webdev, cheatsheet
Published: 2020-07-31 05:52:21

##[Object.assign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
copies properties from one or more source objects to target object

```javascript
// example
Object.assign({ a: 1, b: 2 }, { c: 3 }, { d: 4 }) // { a: 1, b: 2, c: 3, d: 4 }
// syntax
Object.assign(target, ...sources)
```

______

##[Object.create()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create)
creates new object, using an existing object as the prototype

```javascript
// example
Object.create({ a: 1 }) // <prototype>: Object { a: 1 }
// syntax
Object.create(proto, [propertiesObject])
```

______

##[Object.defineProperties()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties)
defines new or modifies existing properties

```javascript
// example
Object.defineProperties({ a: 1, b: 2 }, { a: {
  value: 3,
  writable: true,
}}) // { a: 3, b: 2 }
// syntax
Object.defineProperties(obj, props)
```

______

##[Object.defineProperty()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty)
defines new or modifies existing property

```javascript
// example
Object.defineProperty({ a: 1, b: 2 }, 'a', {
  value: 3,
  writable: true
}); // { a: 3, b: 2 }
// syntax
Object.defineProperty(obj, prop, descriptor)
```

______

##[Object.entries()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)
returns array of object's [key, value] pairs

```javascript
// example
Object.entries({ a: 1, b: 2 }) // [ ["a", 1], ["b", 2] ]
// syntax
Object.entries(obj)
```

______

##[Object.freeze()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)
freezes an object, which then can no longer be changed

```javascript
// example
const obj = { a: 1 }
Object.freeze(obj)
obj.prop = 2 // error in strict mode
console.log(obj.prop) // 1
// syntax
Object.freeze(obj)
```

______

##[Object.fromEntries()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries)
transforms a list of key-value pairs into an object

```javascript
// example
Object.fromEntries([['a', 1], ['b', 2]]) // { a: 1, b: 2 }
// syntax
Object.fromEntries(iterable)
```

______

##[Object.getOwnPropertyDescriptor()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor)
returns a property descriptor for an own property

```javascript
// example
const obj = { a: 1 }
Object.getOwnPropertyDescriptor(obj, 'a') // { value: 1, writable: true, enumerable: true, configurable: true }
// syntax
Object.getOwnPropertyDescriptor(obj, prop)
```

______

##[Object.getOwnPropertyDescriptors()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptors)
returns all own property descriptors

```javascript
// example
const obj = { a: 1 }
Object.getOwnPropertyDescriptors(obj, 'a') // { a: { value: 1, writable: true, enumerable: true, configurable: true } }
// syntax
Object.getOwnPropertyDescriptor(obj, prop)
```

______

##[Object.getOwnPropertyNames()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames)
returns array of all properties

```javascript
// example
Object.getOwnPropertyNames({ a: 1, b: 2 }) // [ "a", "b" ]
// syntax
Object.getOwnPropertyNames(obj)
```

______

##[Object.getOwnPropertySymbols()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols)
array of all symbol properties

```javascript
// example
const obj = { a: 1 }
const b = Symbol('b')
obj[b] = 'someSymbol' // obj = { a: 1, Symbol(b): "symbol" }
Object.getOwnPropertySymbols(obj) // [ Symbol(b) ]
// syntax
Object.getOwnPropertySymbols(obj)
```

______

##[Object.getPrototypeOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf)
returns the prototype

```javascript
// example
const proto = { a: 1 }
const obj = Object.create(proto)
obj.b = 2 // obj = { b: 2 }
Object.getPrototypeOf(obj) // { a: 1 }
// syntax
Object.getPrototypeOf(obj)
```

______

##[Object.is()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)
determines whether two values are the same value

```javascript
// example
const objA = { a: 1 }
const objB = { a: 1 }
Object.is(objA, objA) // true
Object.is(objA, objB) // false
Object.is('a', 'a') // true
// syntax
Object.is(value1, value2)
```

______

##[Object.isExtensible()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isExtensible)
determines wether an object can have new properties added to it

```javascript
// example
const obj = {}
Object.isExtensible(obj) // true
Object.preventExtensions(obj)
Object.isExtensible(obj) // false
// syntax
Object.isExtensible(obj)
```

______

##[Object.isFrozen()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isFrozen)
determines if an object is frozen

```javascript
// example
const obj = {}
Object.isFrozen(obj) // false
Object.freeze(obj)
Object.isFrozen(obj) // true
// syntax
Object.isFrozen(obj)
```

______

##[Object.isSealed()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isSealed)
determines if an object is sealed

```javascript
// example
const obj = {}
Object.isSealed(obj) // false
Object.seal(obj)
Object.isSealed(obj) // true
// syntax
Object.isSealed(obj)
```

______

##[Object.keys()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)
returns array of object's enumerable property names

```javascript
// example
Object.keys({ a: 1, b: 2 }) // [ "a", "b" ]
// syntax
Object.keys(obj)
```

______

##[Object.preventExtensions()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/preventExtensions)
prevents new properties from being added to an object

```javascript
// example
const obj = { a: 1 }
Object.preventExtensions(obj)
Object.defineProperty(obj, 'b', { value: 2 }) // Error: Can't define property "b": Object is not extensible
// syntax
Object.preventExtensions(obj)
```

______

##[Object.prototype.hasOwnProperty()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)
returns boolean indicating whether object has the specified property

```javascript
// example
const obj = { a: 1 }
obj.hasOwnProperty('a') // true
obj.hasOwnProperty('b') // false
// syntax
obj.hasOwnProperty(prop)
```

______

##[Object.prototype.isPrototypeOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf)
checks if object exists in another object's prototype chain

```javascript
// example
const proto = { a: 1 }
const obj = Object.create(proto)
proto.isPrototypeOf(obj) // true
// syntax
prototypeObj.isPrototypeOf(object)
```

______

##[Object.prototype.propertyIsEnumerable()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable)
checks whether the specified property is enumerable and is the object's own property

```javascript
// example
const obj = { a: 1 } const arr = ['a']
obj.propertyIsEnumerable('a') // true
arr.propertyIsEnumerable(0) // true
arr.propertyIsEnumerable('length') // false
// syntax
obj.propertyIsEnumerable(prop)
```

______

##[Object.prototype.toString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString)
returns a string representing the object

```javascript
// example
const obj = {}
obj.toString() // "[object Object]"
const arr = ['a', 'b']
arr.toString() // "a,b"
// syntax
obj.toString()
```

______

##[Object.seal()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal)
prevents new properties from being added and marks all existing properties as non-configurable

```javascript
// example
const obj = { a: 1 }
Object.seal(obj)
obj.a = 2 // { a: 2 }
obj.b = 3 // error in strict mode
delete obj.a // error in strict mode
// syntax
Object.seal(obj)
```

______

##[Object.values()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values)
returns array of object's own enumerable property values

```javascript
// example
Object.values({ a: 1, b: 'a'}) // [ 1, "a" ]
// syntax
Object.values(obj)
```

______