Search⌘ K
AI Features

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.

Exercise 1:

Swap two variables using one destructuring assignment.

Node.js
let text1 = 'swap';
let text2= 'me';
//Write Code here

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.

...