Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 1

Spread operator with arrays, Sets and Maps in JavaScript

26 Spreading into an array literal

easy

Join and copy arrays with ... inside an array literal, then spread a string, a Set and a Map. Check what a spread does to a hole, and what it refuses to do to a plain object.

Stuck? Three hints, smallest first
  1. 1. [...a, ...b] replaces a.concat(b), and a spread may sit anywhere in the literal.
  2. 2. Anything with an iterator can be spread: strings, Sets, Maps, generators, arguments.
  3. 3. A plain object has no iterator. Spreading one into an array throws; into an object literal it is fine.

The answer

const attempt = (fn) => {
  try { return fn(); } catch (e) { return e.constructor.name; }
};

const week1 = [1200, 900];
const week2 = [1500];

const joined = [...week1, ...week2];
const copy = [...week1];
copy.push(0);                                    // must not reach week1
const surrounded = [0, ...week1, 9999];

const letters = [...'Yaba'];
const unique = [...new Set([1, 1, 2, 2, 3])];
const pairs = [...new Map([['A1', 12000]])];

const holey = [1, , 3];                          // index 1 is a hole
const filled = [...holey];

const astral = 'a' + String.fromCodePoint(0x1d7d8);   // one letter, one astral digit
const intoArray = attempt(() => [...{ code: 'A1' }]);

console.log('joined     :', joined);
console.log('copy       :', copy, 'source still', week1);
console.log('anywhere   :', surrounded);
console.log('a string   :', letters);
console.log('a Set      :', unique, '| a Map:', pairs);
console.log('hole before:', 1 in holey, 'after:', 1 in filled);
console.log('code units :', astral.length, 'code points:', [...astral].length);
console.log('an object  :', intoArray);

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('two arrays go into one literal', joined, [1200, 900, 1500]);
test('a spread copy is a new array, so the source is untouched', week1, [1200, 900]);
test('a spread may sit anywhere in the literal', surrounded, [0, 1200, 900, 9999]);
test('a string spreads into characters', letters, ['Y', 'a', 'b', 'a']);
test('a Set spreads de-duplicated, in insertion order', unique, [1, 2, 3]);
test('a Map spreads into entry pairs', pairs, [['A1', 12000]]);
test('a hole becomes a real undefined element', [1 in holey, 1 in filled], [false, true]);
test('spread walks code points, so a surrogate pair stays whole', [astral.length, [...astral].length], [3, 2]);
test('a plain object cannot be spread into an array', intoArray, 'TypeError');

The hole assertion is the one that matters in practice. [1, , 3] has no index 1 at all, so map and forEach skip it; after a spread the slot exists and holds undefined, so they visit it. Spread is therefore a hole-flattening operation, which is usually what you want and occasionally a change in behaviour you did not ask for.

The code-point assertion is the argument for [...str] over str.split(''). split('') cuts between UTF-16 units and will slice an emoji or a mathematical digit in half; the iterator does not.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.