Member-only story

Interview:How do you make var [a, b] = {a: 1, b: 2} deconstruct assignment successfully?✨

Navigate Through This Interview with Advanced JavaScript Destructuring Techniques 🚀

Xiuer Old
3 min readMay 11, 2024
Photo by Danila Perevoshchikov on Unsplash

In the realm of JavaScript, destructuring assignments are a clever shorthand 📝 for extracting array elements or object properties into variables. But what do you do when faced with an interview question that seems to defy logic? The question posed here is not just a test of your coding skills, but also a challenge to your problem-solving abilities. 🤯

When you first glance at the assignment var [a, b] = {a: 1, b: 2}, your initial reaction might be confusion—comparing apples 🍏 to oranges 🍊, right? I felt an equal measure of familiarity and strangeness when I first encountered it. 😭😭😭

In JavaScript, the left side of a destructuring assignment is typically an array, while the right side should be an object with an iterable interface, like an array, Map, Set, etc. Hence, attempting to assign {a: 1, b: 2} to [a, b] throws a syntax error. And yet, the interviewer expects us to make it work—it's a non-verbal yet powerful plea for creativity! 💡

Incorrect Thought Process: ❌

--

--

Xiuer Old
Xiuer Old

Written by Xiuer Old

🔥Little brother teaches front-end and AI online🌈

No responses yet