0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>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.</p>
1
<p>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.</p>
2
<p>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.</p>
2
<p>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.</p>
3
<h2>Array destructuring</h2>
3
<h2>Array destructuring</h2>
4
<p>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.</p>
4
<p>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.</p>
5
<p>Not the most expressive variant - direct reference to array elements by indexes:</p>
5
<p>Not the most expressive variant - direct reference to array elements by indexes:</p>
6
<p>A more interesting option is to destruct the array in the body of the function:</p>
6
<p>A more interesting option is to destruct the array in the body of the function:</p>
7
<p>But you can go even further and add destructuring right into the definition:</p>
7
<p>But you can go even further and add destructuring right into the definition:</p>
8
<p>All the standard rules of array destructuring apply:</p>
8
<p>All the standard rules of array destructuring apply:</p>
9
<p>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:</p>
9
<p>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:</p>
10
<h2>Destructuring of the object</h2>
10
<h2>Destructuring of the object</h2>
11
<p>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:</p>
11
<p>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:</p>
12
<p>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:</p>
12
<p>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:</p>
13
13