concat Merges Without Flattening

concat returns a fresh array merging arguments and spreads values shallowly: nested arrays stay nested and objects are shared by reference with the source.

JavascriptData Fetching

Code

javascriptsnippet
19 lines
// concat merges arrays and bare values into a new array; nested arrays are not flattened
const pageOne = ['a1', 'a2'];
const pageTwo = ['b1', 'b2'];
const merged = pageOne.concat(pageTwo, 'c1', ['c2', ['c3']]);
console.log('merged:', merged);
console.log('pageTwo intact:', pageTwo);

// shallow merge: objects are still shared by reference with the source
const row = { id: 1 };
const withRow = [].concat(row);
withRow[0].id = 99;
console.log('shared row:', row.id);

// nested arrays stay as-is; use flat when flattening is needed
console.log('nested kept:', merged[6]);

// spread syntax is equivalent to concat when joining two arrays
const spread = [...pageOne, ...pageTwo];
console.log('spread same:', spread.join(','));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

merged: [ 'a1', 'a2', 'b1', 'b2', 'c1', 'c2', [ 'c3' ] ]
pageTwo intact: [ 'b1', 'b2' ]
shared row: 99
nested kept: [ 'c3' ]
spread same: a1,a2,b1,b2

How it works

Top-level bindings pageOne, pageTwo, merged, row hold the demo state — nothing sits inside a function scope.

The top-level object literal pins id as plain values — no derivation, no imports behind them.

What to watch for

  • Zero-fetch snippet: every identifier resolves inside the language distribution.

Using it

The run prints merged:, pageTwo intact:, shared row: — each prefix names the step that produced it.

Key statements

const merged = pageOne.concat(pageTwo, 'c1', ['c2', ['c3']]);
const spread = [...pageOne, ...pageTwo];
const withRow = [].concat(row);

FAQ

Are there dependencies to manage?
No. There are no dependencies to declare or install. Its state lives in module-level bindings: `pageOne`, `pageTwo`, `merged`.