destructuring default value code example
Example 1: array destructuring
const myArray = ["a", "b", "c"];
const x = myArray[0];
const y = myArray[1];
console.log(x, y);
const myArray = ["a", "b", "c"];
const [x, y] = myArray;
console.log(x, y);
Example 2: destructuring assignment
Destructuring assignment is special syntax introduced in ES6,
for neatly assigning values taken directly from an object.
const user = { name: 'John Doe', age: 34 };
const { name, age } = user;
Example 3: object destructuring
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
console.log(b);
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
Example 4: js object destructuring with defaults
const { dogName = 'snickers' } = { dogName: undefined }
console.log(dogName)
const { dogName = 'snickers' } = { dogName: null }
console.log(dogName)
const { dogName = 'snickers' } = { dogName: false }
console.log(dogName)
const { dogName = 'snickers' } = { dogName: 0 }
console.log(dogName)
Example 5: object destructuring default value
let a, b;
[a=5, b=7] = [1];
console.log(a);
console.log(b);