1You can use "defaults" in destructuring as well:
2
3(function test({a = "foo", b = "bar"} = {}) {
4 console.log(a + " " + b);
5})();
6
7This is not restricted to function parameters,
8but works in every destructuring expression.
1function f() {
2 return [1, 2];
3}
4
5let a, b;
6[a, b] = f();
7console.log(a); // 1
8console.log(b); // 2
9
1const array = ['ismail', 'sulman'];
2// array destructuring
3const [firstElement, secondElement] = array;
4console.log(firstElement, secondElement);
5const secondArray = ['ismail', 'naeem', 'Mr', 1];
6const [firstEl, secondEl, ...array1] = secondArray;
7console.log(firstEl, secondEl, array1);
1// Taken from top stack overflow answer
2
3const { dogName = 'snickers' } = { dogName: undefined }
4console.log(dogName) // what will it be? 'snickers'!
5
6const { dogName = 'snickers' } = { dogName: null }
7console.log(dogName) // what will it be? null!
8
9const { dogName = 'snickers' } = { dogName: false }
10console.log(dogName) // what will it be? false!
11
12const { dogName = 'snickers' } = { dogName: 0 }
13console.log(dogName) // what will it be? 0!