Exercise on Destructuring
Explore practical exercises on ES6 destructuring to simplify variable swaps, extract array elements, and manage object defaults. Learn to write cleaner JavaScript with fewer lines and improved readability through hands-on tasks.
We'll cover the following...
We'll cover the following...
Exercise 1:
Swap two variables using one destructuring assignment.
Explanation
The text1 = text2 and the text2 = text1 assignments take place in parallel from the perspective of the whole expression. The expression on the right is evaluated, and becomes [ 'me', 'swap' ]. This evaluation happens before interpreting the expression on the left.
...