Destructuring and Spreading

by clarmond

JavaScript

const testObj = {
	a: [1, 2, 3],
  b: [4, 5, 6]
}

let { a } = testObj;
let { b } = testObj;
let { c } = testObj;

console.log(a); // Should be [1, 2, 3]
console.log(b); // Should be [4, 5, 6]
console.log(c); // Should be undefined

const ab = [...a, ...b];
console.log('ab', ab) // Should be [1, 2, 3, 4, 5, 6]

try {
  const ac = [...c, ...a];
  console.log('ac', ac) // Throws an error because c is undefined
} catch (e) {
	console.warn(e);
}

if (c === undefined) {
	c = [];
  console.log('ac', [...a, ...c]);
}