because the variables are being created anew so they need to be declared with const
or let
, you don’t use const
or let
when assigning a value to an already existing variable. In the editor a
and b
have already been declared at the top, so you don’t need to declare them again, nolet
or const
is neede.
why is this creating you so much confusion? with let [c, d] = [1,2,3,4]
only the first two items in the array are being destructure, the others are ignored. It dosn’t make a difference how many ignored items there are. With a situation like let [c, d] = [1, 2]
there are just 0 ignored items.
the variables hold values that are numbers, it is exactly the same. You use variables to have reusable code, but the behaviour is the same using a literal or a variable. Don’t panic if I say that there could be objects, functions, other arrays, in the array and it would work the same, it is just that numbers are easy to type and make easy examples. The elements in the array could be whatever, it doesn’t matter. Destructuring takes the elements in the array and store them in variables. The way that it is written is frankly incohomprensible, but that is the evolution of a programming language, new versions add features that let you write less, but you loose cohomprensibility.
because they are literally that data type, you can look at it and see that it is a string, number, array, object. It is to distinguish those from values held in variables.
it is not considered destructuring
that’s exactly an example of destructuring, with the caveat that it will work exactly like only if x
and y
are already declared variables, if they are new you need to put a let
or const
in front of the line
destructuring is one of the new features of the language that have obscure syntax, don’t try to intepret them using previous knowledge. Think of it as a totally new thing, with its own syntax. To the right of the assignment operator there is an array, to the left of the assignment operator a completely new grammar rule that you can’t compare in any way to something you already know.