JavaScript, inside out · Chapter 15 of 51
Destructuring
By the end of this chapter you can pull values out of arrays and objects into named variables in one line, with defaults for what is missing, and write a function that takes named options. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The menu
- 5 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
Ravi's kitchen screen calls out who is being served, who is next and who is waiting. Anjali's receipt line should count one cup when no quantity is given, and the delivery screen needs the area from deep inside an order, with the stall in Indiranagar when there is none. Meera wants two places in the queue swapped, and an order function that nobody can call with its arguments in the wrong order.
With what you know so far, every value needs its own line: list[0], list[1], order.customer.address.area and so on, each with its own check for what might be missing. A function that takes four values in a row lets a caller swap the size and the sugar without any error at all.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
callQueue: now serving, next, and everyone else
Write callQueue(list). Return an object with three keys: now (the first name in the queue), next (the second) and rest (a new array of everyone after those two, in queue order): callQueue(['Ravi', 'Anjali', 'Meera', 'Arjun']) gives { now: 'Ravi', next: 'Anjali', rest: ['Meera', 'Arjun'] }.
rest is always an array: a queue of two gives rest [], a queue of one gives next undefined and rest [], and an empty queue gives { now: undefined, next: undefined, rest: [] }. Do not change the queue you are given (the check passes a frozen one).
- Task 2
receiptLine: one line per order, one cup by default
Write receiptLine(order). The order has a customer, an item, a price and maybe a qty. Return the text Ravi: 2 x Masala chai = ₹80: the customer, a colon and a space, the qty, a space, x, a space, the item, then a space, =, a space, ₹ and the qty times the price.
When qty is missing, or is undefined, count one cup: receiptLine({ customer: 'Meera', item: 'Parle-G', price: 10 }) gives Meera: 1 x Parle-G = ₹10.
- Task 3
deliveryArea: the area from deep inside an order
Write deliveryArea(order). The order has a customer with a name and maybe an address, and the address may have an area. Return the customer's name, a comma and a space, then the area: deliveryArea({ id: 1, customer: { name: 'Anjali', address: { area: 'Koramangala' } } }) gives Anjali, Koramangala.
When the customer has no address, or the address has no area, the area is Indiranagar, where the stall is: deliveryArea({ id: 2, customer: { name: 'Ravi' } }) gives Ravi, Indiranagar.
- Task 4 · interview question
Interview: swapPlaces, two places swapped on a new array
Write swapPlaces(list, i, j). Return a new array with the items at positions i and j swapped and everything else where it was: swapPlaces(['Ravi', 'Anjali', 'Meera'], 0, 2) gives ['Meera', 'Anjali', 'Ravi']. Swapping a position with itself gives an unchanged copy.
The result is always a new array, and the list you are given must not change (the check passes a frozen one).
- Task 5
makeOrder: an order from named options with defaults
Write makeOrder(options). Every option has a default: item 'Masala chai', qty 1, size 'regular' and sugar 'normal'. Return a new object with exactly those four keys, each taken from the options or its default: makeOrder({ qty: 2, sugar: 'less' }) gives { item: 'Masala chai', qty: 2, size: 'regular', sugar: 'less' }.
Calling with no argument at all, or with {}, gives all four defaults. An option holding undefined gets its default too. Any other key in the options (such as a note) is left out, and the options object is neither changed nor returned.
What you will learn
- Array destructuring: take values by position, skip some, and gather the rest.
- Object destructuring: take values by key name, rename them and give defaults.
- Nested patterns and swaps: reach deep inside, and exchange two values in one line.
- Destructured parameters: a function that takes one object of named options.
More in The menu
Build "Destructuring" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.