Function parameters destructuring in JavaScript

Destructuring has another very important application area - passing function parameters. The bottom line is this: if a function takes an array as a parameter, we can set directly in the function declaration how to destructure this array.

Let's look at an example. Suppose we have a function that takes an array with a year, month and day as a parameter:

func([2025, 12, 31]);

Let's specify directly in the function parameter which variables this array should be split into:

function func([year, month, day]) { console.log(year); // shows 2025 console.log(month); // shows 12 console.log(day); // shows 31 }

The above construction should be considered as a single function parameter. You can add more parameters if you want:

func('str1', [2025, 12, 31], 'str2'); function func(param1, [year, month, day], param2) { console.log(param1); // shows 'str1' console.log(year); // shows 2025 console.log(month); // shows 12 console.log(day); // shows 31 console.log(param2); // shows 'str2' }

And in the following example, arrays are passed as the first and second parameters to the function, and we destructure both of them:

func([2025, 12, 31], [2026, 11, 30]); function func([year1, month1, day1], [year2, month2, day2]) { console.log(year1); // shows 2025 console.log(month1); // shows 12 console.log(day1); // shows 31 console.log(year2); // shows 2026 console.log(month2); // shows 11 console.log(day2); // shows 30 }

Rework the following code through destructuring according to the theory you learned:

function func(employee) { let name = employee[0]; let surname = employee[1]; let department = employee[2]; let position = employee[3]; let salary = employee[4]; } func( ['John', 'Smit', 'development', 'programmer', 2000] );

Rework the following code through destructuring according to the theory you learned:

function func(employee) { let name = employee[0]; let surname = employee[1]; let info = employee[2]; } func( ['John', 'Smit', 'development', 'programmer', 2000] );

Rework the following code through destructuring according to the theory you learned:

function func(employee) { let name = employee[0]; let surname = employee[1]; let department = employee[2]; let position; if (arr[3] !== undefined) { position = arr[3]; } else { position = 'junior'; } } func( ['John', 'Smit', 'development'] );

Rework the following code through destructuring according to the theory you learned:

function func(department, employee, hired) { let name = employee[0]; let surname = employee[1]; let year = hired[0]; let month = hired[1]; let day = hired[2]; } func( 'development', ['John', 'Smit'], [2018, 12, 31] );

Objects in parameters

Similarly, you can destructure parameter-objects:

function func({year, month, day}) { console.log(year); // shows 2025 console.log(month); // shows 12 console.log(day); // shows 31 } func({year: 2025, month: 12, day: 31,});

Rework the following code through destructuring according to the theory you learned:

function func(options) { let color = options.color; let width = options.width; let height = options.height; } func( {color: 'red', width: 400, height: 500} );

Rework the following code through destructuring according to the theory you learned:

function func(options) { let width = options.width; let height = options.height; let color; if (options.color !== undefined) { color = options.color; } else { color = 'black'; } } func( {color: 'red', width: 400, height: 500} );
enru