So I was reading an article to clone an object and array. All of them mentioned that Object.assign() can be used to copy an object but no one mentioned that Object.assign() will also work to shallow copy an array.
Could anybody explain how it works?
var a = ['a','b','c'];
var b = Object.assign([],a,['x']);
// Checking the Type of Variable a
console.log(Array.isArray(a))
// Checking the Type of Variable b
console.log(Array.isArray(b))
console.log('Output of B:', b)
// variable b is a clone of variable a and also have differnet memory allocation.
a[1] = 'changed again';
// Checking Changes of variable b
console.log('Output of B:', b)
// Checking Changes of variable a
console.log('Output of A:', a)