JS Coding Questions Logo
JS Coding Questions
#316💼 Interview💻 Code

What are default values in destructuring assignment

Advertisement

728x90

A variable can be assigned a default value when the value unpacked from the array or object is undefined during destructuring assignment. It helps to avoid setting default values separately for each assignment. Let's take an example for both arrays and object use cases,

Arrays destructuring:

javascript
1var x, y, z;
2
3  [x = 2, y = 4, z = 6] = [10];
4
5console.log(x); // 10
6
7console.log(y); // 4
8
9console.log(z); // 6

Objects destructuring:

javascript
1var { x = 2, y = 4, z = 6 } = { x: 10 };
2
3console.log(x); // 10
4
5console.log(y); // 4
6
7console.log(z); // 6

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 59

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
316of476