Variable declaration when destructuring an array in JavaScript
It is not necessary to declare variables
when destructuring. They can be declared
in advance and then when assigning it will
not be necessary to write the
command let:
let arr = [2025, 12, 31];
let year;
let month;
let day;
[year, month, day] = arr;
There are, however, some nuances. Look at the following code:
let arr = [2025, 12, 31];
let year;
let [year, month, day] = arr;
As you can see, the variable year
has been declared in advance, but the
variables month and day
have not. So the author of the code
decided to write let before
the destructuring assignment.
This, however, will lead to an error,
since it is impossible to declare the
same variable twice through let
(it turns out that year is
declared twice).