Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Array Destructuring

Taking elements out of arrays by position.

The Basics

(bind items #a(1 2 3 4 5))
(bind (array first second) items)
(console:log first second)
const items = [1, 2, 3, 4, 5];
const [first, second] = items;
console.log(first, second);     // 1 2

Skipping Elements with _

_ skips a position without binding:

(bind (array _ _ third) items)
const [, , third] = items;      // third = 3

Note: _ as a skip marker only works in array patterns. In object patterns, _ is a regular binding name.

Collecting Remaining Elements

(bind (array head (rest tail)) items)
const [head, ...tail] = items;  // head = 1, tail = [2, 3, 4, 5]

rest collects all remaining elements into an array. It must be the last element in the pattern.

Swapping Variables

A classic destructuring trick — no temporary variable needed:

(bind (array b a) (array a b))
[b, a] = [a, b];