General JavaScript Notes
General Object Destructuring Example
Now the variables listed in between the curly braces are assigned the value of their respective properties in myObj.
The order in which the variables are listed in the curly braces doesn't matter. Additionally, we don't have to list all the properties of an object if we only need one or two.
General Array Destructuring Example
Unlike objects, the name we give the variables doesn't matter. Let's change the above example: So, each of the variable names will ONLY count for the index-positions I fetch.
If I include less variables then there are indexes in the arrays, then just like in Object-destructuring, only that many array element will be included in the returned array, starting from zero-index position and AGAIN without giving any meaning to the the name I give to the variables. So, each of the variable names will ONLY count for the index-positions I fetch.
let arr = ['Jim', 'Bob', 'Sarah', 'Cassie'];
let [ jim, bob, cassie ] = arr;
console.log(jim, bob, cassie); //outputs: Jim Bob Sarah
Using Spread operator - It is often used for splitting out a part of an object, but keeping the remaining properties in another object.
Last updated