Technical skill practice

JavaScript trimEnd: Remove Trailing Whitespace Without Changing the Label

Use trimEnd to remove trailing whitespace while preserving the beginning of a string. Use trim when both ends should be normalized. Neither operation removes ordinary spaces inside the text.

The task

Clean a fictional imported shelf label with a leading space, an interior double space and trailing space, tab and newline. Verify exactly which characters remain.

Runnable Node 22 example

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

const imported = '  west  shelf \t\n';
const endOnly = imported.trimEnd();
assert.equal(endOnly, '  west  shelf');
assert.equal(imported.trim(), 'west  shelf');
assert.equal(imported.endsWith('\n'), true);
assert.equal(endOnly.includes('west  shelf'), true);
assert.equal('labelX'.trimEnd(), 'labelX');
console.log(JSON.stringify(endOnly));

Expected output and explanation

The trailing whitespace sequence disappears as a group, not just a final ordinary space. The leading spaces survive trimEnd, while trim removes them too. The double interior space remains in both cases.

The unchanged labelX check distinguishes whitespace trimming from removing an arbitrary suffix character. If the last letter or punctuation mark needs removal, use a different operation with a stated rule rather than expecting trimEnd to erase it.

Output from the example

" west shelf"

Pitfall to check

Do not trim fields whose leading or trailing whitespace is meaningful to the format. Also keep normalization separate from emptiness validation: after trimming, decide whether an empty result is acceptable.

Questions, answered

Does trimEnd mutate the original string?

No. It returns a string result. Retain or assign that result explicitly if the cleaned label should replace the imported value.

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.