In the dynamic world of JavaScript, destructuring has emerged as a powerful tool, enabling developers to extract properties from arrays or objects in a more efficient and readable manner. One of its most useful features is the ability to set default values for the extracted properties. Let's dive into the concept of JavaScript destructuring with default values.

Before we delve into the details, let's ensure we're on the same page regarding JavaScript destructuring. It's a feature that allows us to extract values from an array or object and assign them to variables in one line of code. It's like unwrapping a present, but with code!

Understanding Destructuring with Default Values
Now, what about those default values you've heard about? In JavaScript, when destructuring, if you attempt to extract a property that doesn't exist, you'll get `undefined`. However, you can set a default value that will be used in such cases, enhancing the robustness of your code.

Imagine you're extracting properties from an object, but you're not sure if all properties exist. With default values, you can ensure that if a property is missing, a certain value will be used instead. For example:
```javascript let { foo = 'default', bar } = { bar: 'hello' }; console.log(foo); // 'default' console.log(bar); // 'hello' ```
Setting Default Values in Objects

In the above example, we're setting a default value for `foo`. If `foo` doesn't exist in the object, it will take the value of 'default'. Let's explore this further:
```javascript let obj = { bar: 'world' }; let { foo = 'default', bar } = obj; console.log(foo); // 'default' console.log(bar); // 'world' ```
As you can see, `foo` took the default value because it didn't exist in the `obj` object.
Setting Default Values in Arrays

Destructuring works with arrays, too. If you're extracting elements from an array but the indexes are beyond the array's length, you can set a default value:
```javascript let arr = [1, 2]; let [a, b = 3] = arr; console.log(a); // 1 console.log(b); // 3 ```
Here, `b` takes the default value of 3 because the second index in `arr` doesn't exist.
Handling Complex Structures

Often, you'll need to extract properties from deeply nested objects or arrays. For these cases, you can use the rest property (`...`) to capture all remaining values without losing them:
For objects:




















```javascript let { a, ...rest } = { a: 1, b: 2, c: 3 }; console.log(a); // 1 console.log(rest); // { b: 2, c: 3 } ```
For arrays:
```javascript let [a, ...rest] = [1, 2, 3, 4]; console.log(a); // 1 console.log(rest); // [2, 3, 4] ```
In both examples, we're extracting the first property/element and capturing the rest into a new variable. Now, you should be well-equipped to handle complex structures with destructuring and default values.
Combining Destructuring and Default Values
You can also combine destructuring and default values in complex structures. Let's say you have a deeply nested object, and you want to set a default value for a property two levels deep:
```javascript let obj = { a: { b: 1 } }; let { a: { c = 2 } } = obj; console.log(c); // 2 ```
Here, `c` takes the default value of 2 because it doesn't exist in the `obj` object.
Embrace destructuring with default values β it's a powerful tool that will make your JavaScript code cleaner, more readable, and more robust.