JS: Functions
Theory: Parameter destructuring
When we pass an argument when we call a function, its value is assigned to the function parameter. This is an implicit automatic assignment, because this operation is "not visible" in the code.
Passing an argument is no different from simply assigning a value to a variable. This means that we have access to destructuring right in the function definition.
Array destructuring
Let's write a function that takes as input an array of two elements and prints them to the terminal. Let's look at different ways of implementing parameter handling.
Not the most expressive variant - direct reference to array elements by indexes:
A more interesting option is to destruct the array in the body of the function:
But you can go even further and add destructuring right into the definition:
All the standard rules of array destructuring apply:
If there are less than two elements in the passed array, the parameters that "lacked" the corresponding values will contain undefined. For such cases, you can insure and set a default value:
Destructuring of the object
Let's write a function that takes as input an object with information about the name and surname of a user and outputs it to the terminal. Immediately we implement the variant with destructuring of the object for parameters:
A typical situation in practice is when an object with many properties comes to the input of a function, but in fact we do not need the values of all properties, but only a few. This can happen, for example, when processing an HTTP response from a server or configuration for a program. In such cases, we take away only the values we need - because we don't have to specify all of the object's properties when destructuring:

