Technical skill practice
Check Whether a JavaScript String Is Empty: State the Whitespace Rule
An empty-string check and a whitespace-only check are different. Decide whether trimming belongs to the field’s rule. Also check the input type before calling string methods on null or a missing value.
The task
Accept a nonblank string label after trimming its ends. Keep internal spaces, and return null for unsupported values rather than treating them as labels.
Runnable Node 22 example
const assert = require('node:assert/strict');
function label(value) {
if (typeof value !== 'string') return null;
const cleaned = value.trim();
return cleaned.length === 0 ? null : cleaned;
}
assert.equal('' === '', true);
assert.equal(' ' === '', false);
assert.equal(label(' '), null);
assert.equal(label(null), null);
assert.equal(label(undefined), null);
assert.equal(label(' east wing '), 'east wing');
console.log(JSON.stringify(['', ' ', null, ' east wing '].map(label)));
Expected output and explanation
The strict empty-string comparison rejects only zero-length text; three spaces are still a string with content. The helper deliberately adopts a stronger rule and checks the trimmed result. It retains the two interior spaces in east wing because this contract only trims the ends.
Null and undefined fail the type guard before trim runs. That keeps the input contract explicit instead of converting every value with String and accidentally accepting the text “null” as a meaningful label.
Output from the example
[null,null,null,"east wing"]
Pitfall to check
Do not reuse this normalization for every field. Passwords, fixed-width records and other inputs can treat whitespace differently. If the product needs a maximum length, specify its counting unit separately rather than assuming nonblank also means valid.
Questions, answered
Is if (!value) an equivalent empty-text check?
No. It also matches values such as false and zero and does not reject a whitespace-only string. Use the check that matches the field’s stated types and whitespace policy.
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.