Copying using spread operator or assign would be mainly useful when using content from constant files, and you want to make sure none of the content is overwritten during the business logic.
1. One use would be to make a copy of an array.
const x= [1,2,3,4]
if we do,
const y = x; // this will only give another reference i.e x[1] = 6 then also y[1] would print 6.
So to copy
2. Combine 2 arrays or merging two arrays become much easier with the spread operator.
eg:
const a = {1: 'hello'}
const b = {2: 'there'}
const c = {...a, ...b}
output: c {1: 'hello', 2:'there'}
With ES5 we would have to use something like assign function to update an existing array.
const x= [1,2,3,4]
if we do,
const y = x; // this will only give another reference i.e x[1] = 6 then also y[1] would print 6.
So to copy
2. Combine 2 arrays or merging two arrays become much easier with the spread operator.
eg:
const a = {1: 'hello'}
const b = {2: 'there'}
const c = {...a, ...b}
output: c {1: 'hello', 2:'there'}
With ES5 we would have to use something like assign function to update an existing array.
So far so good, we know when to use assign or spread operator.
Suppose, we have an object which is not supposed to change throughout then we can go for Object.freeze();
This freeze function will ensure that the values inside it are never changed, and any attempt to do so will throw an error.
Thats all for this post :)