In Arrays: In Arrays, values of corresponding elements get stored in the variables.
Example 1: In order to give default values in arrays when applying the destructuring concept in arrays, we need to initialize values with some value. In this way the default values will be assigned to the variables. Below is the implementation via an example of this concept.
10 20 30
Example 2: If any value is present for the respective variable then it will take that value or else then it will take the default variable. Below code snippet explains the behaviours in a more lucid and elaborative way.
Output: If we are not having ’50’ in that array then ‘c’ would have a ’30’ value.
10 20 50
In Objects: The values of corresponding properties get stored in the variables.
Example 1: The application and implementation of destructuring in objects are also the same as in arrays. Below are the two code snippets on how to use default values in objects.