7. Array and Object Destructuring

Destructuring helps in avoiding the need for temp variables when dealing with object and arrays.

  1. function foo() {
  2. return [1, 2, 3];
  3. }
  4. let arr = foo(); // [1,2,3]
  5. let [a, b, c] = foo();
  6. console.log(a, b, c); // 1 2 3
  7. function bar() {
  8. return {
  9. x: 4,
  10. y: 5,
  11. z: 6
  12. };
  13. }
  14. let { x: a, y: b, z: c } = bar();
  15. console.log(a, b, c); // 4 5 6