What is destructuring in JavaScript — and what [count, setCount] really means

Open any React component an AI wrote for you. Almost certainly it has this line:
const [count, setCount] = useState(0)
Square brackets to the left of =. Looks like an array, but nobody is creating an array here. The opposite — it's being taken apart. This is called destructuring, and after this article you'll read lines like that at a glance.
What destructuring is, in plain words
Destructuring is a way to pull several values out of an object or array in one line and drop them straight into variables.
Picture a parcel. You can open it and take things out one at a time: a key, a coin, a note. Or you can say: "key goes in this tray, coin in that one, note in the third." And everything sorts itself. That second way is destructuring.
Without it, code looks like this:
const name = user.name
const age = user.age
const city = user.city
With it:
const { name, age, city } = user
Same result: three variables holding the right values. Just shorter.
Two kinds: curly and square brackets
The brackets on the left tell you what you're taking apart.
Curly { } — for objects. You pull fields out by name. Order doesn't matter, the name has to match:
const user = { name: "Anna", age: 25 }
const { age, name } = user // name = "Anna", age = 25
Ask for a name the object doesn't have and you get undefined. No error — just empty.
Square [ ] — for arrays. Here it's the reverse: names are up to you, order matters. The first variable gets the first item, the second gets the second:
const [first, second] = ["coffee", "tea", "juice"]
// first = "coffee", second = "tea"
Now look at useState again. It returns an array of two things: the current value and a function that changes it. const [count, setCount] = useState(0) puts the first into count and the second into setCount. The names were picked by whoever wrote the code. [apples, setApples] would work exactly the same. More on that pair in what is state.
Handy tricks AI writes all the time
These show up constantly in generated code. One at a time.
A default value. If the field is missing, use a fallback:
const { role = "guest" } = user
// no role in user → role = "guest"
Renaming. The field is called one thing, you want the variable called another:
const { name: userName } = user
// userName now holds user.name
Read the colon as "put into". name: userName means "put the name field into userName". This is the most common mix-up: it looks like a type on the right, but it's a new name.
Destructuring right in a function's parameters. This is React's main move:
function Card({ title, price }) {
return <div>{title}: ${price}</div>
}
The component receives one object of properties. The curly brackets in the parameters pull title and price out of it immediately.
Skip an item. In an array you can leave a blank with a comma: const [x, , z] = [1, 2, 3] — x is 1, z is 3.
Swap two variables without a third: [a, b] = [b, a]. Looks like a magic trick, but it's just array destructuring.
The error you'll definitely see
Sooner or later the console will show:
TypeError: Cannot destructure property 'name' of 'user' as it is undefined.
Translation: "I can't pull name out of user, because user is undefined." You're trying to open a parcel that isn't there.
It usually happens when data hasn't arrived from the server yet. The component is already rendering, but user is still empty. You can't take apart nothing — only an object or an array.
How to fix it:
- Provide a fallback object:
const { name } = user ?? {}. Ifuseris empty, you destructure an empty object and getundefinedinstead of a crash. More on??in what is null. - Default the parameter:
function Card({ title } = {}). - Don't render until the data is there: check
if (!user) return nullbefore destructuring.
Is destructuring slower than normal access?
No, not in any way you'd notice. It's just a shorter way to write the same user.name. Pick whatever reads easier.
Does Python have this?
Something similar, called unpacking: a, b = pair. For dictionaries there's no short form like JavaScript's — people usually write data["name"].
Do I need to write destructuring myself?
Not necessarily, but you do need to read it. AI uses it in nearly every file. When you see brackets to the left of =, it's taking apart, not creating.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





