Technical skill practice

JavaScript concat: Combine Arrays Without Recursive Flattening

Array concat returns a combined array. Ordinary array arguments contribute their elements, but nested arrays inside those elements remain nested. Combining containers does not clone the objects they reference.

The task

Combine an early-shift note with a later note and a nested group. Then edit the shared early-shift note to observe which data remains connected.

Runnable Node 22 example

const assert = require('node:assert/strict');

const note = {text: 'inspect gate'};
const early = [note];
const late = [{text: 'check lights'}];
const grouped = [['desk', 'store']];
const combined = early.concat(late, grouped);
assert.equal(combined.length, 3);
assert.deepEqual(combined[2], ['desk', 'store']);
assert.equal(early.length, 1);
assert.notEqual(combined, early);
combined[0].text = 'inspect west gate';
assert.equal(note.text, 'inspect west gate');
assert.equal(combined[0], early[0]);
console.log(JSON.stringify(combined));

Expected output and explanation

The result has three elements: two note objects and one nested array. concat spreads the outer grouped array by one level, so the inner pair remains a single element. This differs from recursively flattening every nested sequence.

The early container still has one element. Editing the first result object reaches the same note referenced by early, which explains the changed text without implying that concat inserted an item into the source. A new outer array can still contain shared data.

Output from the example

[{"text":"inspect west gate"},{"text":"check lights"},["desk","store"]]

Pitfall to check

Calling string concat produces text and follows a different contract. Check the receiver before interpreting the result. Avoid using concat as a hidden deep-copy step when independent nested records are required.

Questions, answered

How can I append an entire array as one element?

Wrap it in another array for this ordinary-array case, or use a deliberate push on the target. Test the resulting nesting against the shape your consumer expects.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.