Technical skill practice

JavaScript Array push: Append Items and Inspect the Returned Length

push appends elements to its array and returns the resulting length. It does not return the array itself. Passing one array appends that array as one element unless you deliberately expand its elements.

The task

Extend a small processing queue, then compare adding a batch as a nested item with adding its two elements separately.

Runnable Node 22 example

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

const queue = ['scan'];
const size = queue.push('label');
assert.equal(size, 2);
assert.deepEqual(queue, ['scan', 'label']);
const nested = [];
assert.equal(nested.push(['fold', 'pack']), 1);
assert.deepEqual(nested, [['fold', 'pack']]);
const flat = [];
assert.equal(flat.push(...['fold', 'pack']), 2);
assert.deepEqual(flat, ['fold', 'pack']);
console.log(`size=${size}; nested=${nested.length}; expanded=${flat.length}`);

Expected output and explanation

The original queue gains label, while size stores the number two. Using the return as if it were an array would therefore change the variable’s meaning and break a later queue operation.

The nested and expanded cases use the same textual batch but produce different shapes. One receives a single array element; the other receives two string elements. That choice matters when the next consumer expects one work item at each position.

Output from the example

size=2; nested=1; expanded=2

Pitfall to check

push mutates the receiver. If another caller needs the original membership preserved, build a new array deliberately. For large batches, do not assume unlimited function-argument expansion is safe merely because this two-element example works.

Questions, answered

Can I append several values in one push call?

Yes. Each supplied argument becomes an appended element, and the return is the final length after those additions.

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.