Notes on Day 14 of 30 Days of JavaScript

Table of Contents

Lessons Learned

Array.prototype.concat()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

const start = [1, 2, 3, 4, 5]
const end = [6, 7, 8, 9, 10]

start.concat(end)
> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

console.log(start)
> [1, 2, 3, 4, 5]

start.concat()
> [1, 2, 3, 4, 5]

Object.assign()

This method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.

It has the following syntax:

Object.assign(<destination object>, <source object>, ...args)

Some examples:

const square = { sides: 4, color: 'blue' }
const rectangle = Object.assign({}, square, { perimeter: '2L + 2W' })

console.log(rectangle)
> {sides: 4, color: 'blue', perimeter: '2L + 2W'}

console.log(square)
> {sides: 4, color: 'blue'}

For deep copy, it is not recommended especially in React-Redux operations that need to handle complex states.

References

Twitter, LinkedIn