Technical skill practice

JavaScript split: Preserve Empty Fields and Understand the Limit

Use split to turn a string into an array at a specified separator. Repeated separators can produce empty fields. A limit caps the number of returned elements; it does not bundle the unused remainder into the last field.

The task

Inspect a simple pipe-delimited row whose second and final fields are empty. This format deliberately has no quoted separators or escaped field rules.

Runnable Node 22 example

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

const row = 'north||7|';
const fields = row.split('|');
assert.deepEqual(fields, ['north', '', '7', '']);
assert.deepEqual(row.split('|', 2), ['north', '']);
assert.deepEqual('solo'.split('|'), ['solo']);
assert.deepEqual(''.split('|'), ['']);
assert.deepEqual(row.split('|', 0), []);
console.log(JSON.stringify(fields));

Expected output and explanation

The two neighboring pipes leave an empty second field, and the trailing pipe leaves an empty final field. Keeping those positions can matter when columns have meaning. Removing all falsy strings would silently shift later fields into earlier columns.

The two-element limit returns north and the next empty field. It discards the rest of this result rather than returning “7|” as a combined tail. With no matching separator, the entire input becomes one element; an empty source with this separator also produces one empty element.

Output from the example

["north","","7",""]

Pitfall to check

A plain split is not a complete CSV parser. If separators may appear inside quoted fields, define and implement that format or use an appropriate existing parser. This small record’s behavior should not be generalized to every imported file.

Questions, answered

Should I trim each split field?

Only when the format treats surrounding whitespace as insignificant. Trimming is a separate normalization decision and can change meaningful text.

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.