Cool Object methods in JavaScript
Tags: javascript, devjournal, devtips
Published: 2019-12-04 10:46:14

This is a compilation of a few `Object` methods that may come in handy.

- [Object.assign](#objectassign)
- [Object.entries](#objectentries)
- [Object.freeze](#objectfreeze)
- [Object.fromEntries](#objectfromentries)
- [Object.is](#objectis)

## Object.assign

This copies all enumerable own properties from one or more source objects to a target object. So we can use it to add specific properties or change their values by passing other objects to the method. If more than one object have the same key, it will always use the one that's passed last. It returns the target object.

### Syntax

```javascript
Object.assign(target, ...sources)
```

- `target` is the object onto which properties will be copied.
- `sources` are all the objects from which properties will be copied.

### Examples

```javascript
const pony = {
  colour: 'pink',
  species: 'unicorn',
}
Object.assign(pony, { cutieMark: 'star' }, { colour: 'pruple' })
console.log(pony) //{ colour: 'purple', species: 'unicorn', cutieMark: 'star' }
```

`Object.assign` can also be used to create new objects:

```javascript
const pony = {
  colour: 'pink',
  species: 'unicorn',
}
const newPony = Object.assign({}, pony, { cutieMark: 'star' }, { colour: 'pruple' })
console.log(pony) //{ colour: 'pink', species: 'unicorn' }
console.log(newPony)//{ colour: 'purple', species: 'unicorn', cutieMark: 'star' }
```

Since arrays are objects, `Object.assign` also works on them:

```javascript
const array = [ 0, 1, 2, 3, 4, 5 ]
Object.assign(array, { 1: 3, 3: 1 })
console.log(array) //[ 0, 3, 2, 1, 4, 5 ]
```

## Object.entries

This method returns an array of the object's own enumerable properties in `[ key, value ]` pairs.

### Syntax

```javascript
Object.entries(object)
```

### Example

```javascript
const pony = {
  colour: 'pink',
  cutieMark: 'star',
  species: 'unicorn',
}
const arrayedPony = Object.entries(pony)
console.log(arrayedPony) //[[ 'colour', 'pink' ], [ 'cutieMark', 'star'], [ 'species', 'unicorn' ]]
```

*Note* [Object.keys](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) and [Object.values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values) function similarly but only return `keys` and `values` respectively.

## Object.freeze

This one keeps an object from being changed at all. After freezing it, you can't add properties, remove them or change their value. It returns the same object that was passed.

### Syntax

```javascript
Object.freeze(object)
```

### Example

```javascript
const pony = {
  colour: 'pink',
  species: 'unicorn',
}

Object.freeze(pony)
Object.assign(pony, { cutieMark: 'star' }) //Throws error
```

## Object.fromEntries

This method does the opposite of `Object.entries`. It takes an array of `[ key, value ]` pairs and turns them into an object.

### Syntax

```javascript
Object.fromEntries(array)
```

## Object.is

This one compares two values and determines if they are the [same value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness).

This is different from the equality operator (`==`) in that it doesn't do any sort of type coercion.

It is also different from the identity operator (`===`) in that it makes a difference between `-0` and `+0` and it treats `NaN` as equal to `NaN` and to `Number.NaN`.

### Syntax

```javascript
Object.is(value1, value2)
```

### Examples

```javascript
console.log(NaN === NaN) //false
console.log(Object.is(NaN, NaN)) //true

console.log(Number.NaN === NaN) //false
console.log(Object.is(Number.NaN, NaN)) //true

console.log(NaN === +'potato') //false
console.log(Object.is(NaN, +'potato')) //true

console.log(+0 === -0) //true
console.log(Object.is(+0, -0)) //false
```

Two different objects will only be evaluated as `true` if they refer to the same object.

```javascript
const ponyA = {
  colour: 'pink',
  species: 'unicorn',
}

const ponyB = ponyA

console.log(Object.is(ponyA, ponyB)) //true

const ponyC = {
  colour: 'pink',
  species: 'unicorn',
}

console.log(Object.is(ponyA, ponyC)) //false

//But we can still compare their properties' values:
console.log(Object.is(ponyA.colour, ponyC.colour)) //true
```