Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How does JavaScript array destructuring assign values to variables?

Quick answer

Array destructuring assigns values by position: const [first, second] = values puts the first element in first and the second in second. Leave a blank between commas to skip a position, as in const [first, , third] = values. A default such as second = "fallback" applies when that position is undefined or missing, but not when it is null. Destructuring reads values; it does not reshape the array. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

How does JavaScript know which array value goes into each variable?

Geez

It matches by position. In const [name, score] = ["Mina", 92], name receives the first element, "Mina", and score receives the second, 92. The names you choose do not control which elements are selected; their positions in the pattern do. [S1]

The array on the right is the source. The bracketed pattern on the left describes which positions to bind to variables. You can use this with an existing array too:

const results = ["Mina", 92];
const [name, score] = results;

This creates bindings from the values. It does not remove the elements from results or change the array's shape. [S1]

Follow-up question

What if I need the first and third values but not the second?

Geez

Put an extra comma where you want to skip a position: const [first, , third] = values. The blank slot means “do not bind the second position.” For example, with const values = ["red", "green", "blue"], first is "red" and third is "blue". [S1]

The skipped position still counts. If you wrote const [first, third] = values, then third would receive "green", the second element—not the third. Use as many commas as needed to reach the position you want, and name the variable for the value it will actually receive.

Follow-up question

How do defaults work when an array element is missing?

Geez

Write the default after the variable name: const [label = "Untitled"] = values. JavaScript uses that default if the corresponding position produces undefined, including when the element is missing. It does not use the default for an explicit null. [S1]

For example:

const [missing = "fallback"] = [];
const [empty = "fallback"] = [undefined];
const [provided = "fallback"] = [null];

Here, missing and empty become "fallback"; provided remains null. The default applies to that position when its value is undefined or missing. If you need a fallback for null too, that requires a separate choice in your code.

Follow-up question

Can you show a short example that combines positions, a skipped value, and a default?

Geez

Assume this array represents a task as [title, internalCode, owner, status]. You want the title, owner, and status, and you want a fallback when status is missing:

const task = ["Draft guide", "A17", "Lee"];
const [title, , owner, status = "Not set"] = task;

The result is title === "Draft guide", owner === "Lee", and status === "Not set". The second slot is skipped, so owner still comes from position three. The missing fourth value triggers the default. The original task array remains unchanged. [S1]

A useful check before writing the pattern is to number the source positions, then line up each desired variable with its position. Add a blank slot for every position you want to skip, and add a default only where an undefined value should have a fallback.

A practical checklist

  • Identify the source array and number its positions starting at the first element.

  • Place variables in the same order as the values you want to receive.

  • Use a blank slot between commas to skip a position.

  • Add = defaultValue where a missing or undefined value should get a fallback.

  • Check whether an explicit null should remain null or needs separate handling.

  • Confirm that the destructuring pattern reads values without changing the source array.

A few more useful answers

Does array destructuring remove the values it assigns?

No. It binds values from positions in the source; it does not remove elements or reshape the source array. [S1]

Does a destructuring default replace `null`?

No. The default is used for undefined or a missing position, not for an explicit null. [S1]

Does destructuring work only with arrays?

No. Array destructuring works with iterables, not exclusively arrays. [S1]

Sources

References used for this guide.

  1. [S1] Destructuring — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work