October 2018
Intermediate to advanced
590 pages
15h 5m
English
You can apply a destructuring assignment to function parameters as well. Let's see the following example:
1. function workout({gym}, times) {2. console.log('Workout in ' + gym + ' for ' + times + ' times');3. }4. let thisWeek = {gym: 'Gym A'};5. workout(thisWeek, 2); // Workout in Gym A for 2 times
As you can see, in line 1, we use object destructuring syntax to extract the gym variable from the first argument of the workout() function. In this way, the argument passed to the workout() function cannot be null or undefined. Otherwise, TypeError will be thrown. You can pass a number, a string, an array, or a function to the workout() function and JavaScript won't complain about it, although you will get ...
Read now
Unlock full access