Technical skill practice

Check Undefined in JavaScript: Value, Presence and Unknown Names

An undefined value is not the same thing as an absent property. A safe identifier check is also different from directly reading an undeclared name. Choose which question you need to answer before writing the test.

The task

Inspect two settings records and an identifier that this script never declares. Then demonstrate why typeof is not a universal escape from a let binding’s initialization rules.

Runnable Node 22 example

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

const missing = {};
const explicit = {caption: undefined};
assert.equal(missing.caption === undefined, true);
assert.equal(explicit.caption === undefined, true);
assert.equal(Object.hasOwn(missing, 'caption'), false);
assert.equal(Object.hasOwn(explicit, 'caption'), true);
assert.equal(typeof rxNeverDeclared, 'undefined');
assert.throws(() => rxNeverDeclared, ReferenceError);
assert.throws(() => { typeof later; let later; }, ReferenceError);
console.log(`missingOwn=${Object.hasOwn(missing, 'caption')}; explicitOwn=${Object.hasOwn(explicit, 'caption')}`);

Expected output and explanation

Both property reads produce undefined, but only one record owns caption. The presence check therefore preserves information that a value comparison alone loses. This fixture asks about presence rather than supplying a default.

typeof on the never-declared identifier returns the string undefined, while directly reading it throws. The final error shows the important limit: a lexical binding in its temporal dead zone can still throw under typeof. It is not the same situation as an unresolvable name.

Output from the example

missingOwn=false; explicitOwn=true

Pitfall to check

Do not collapse undefined, null and empty text unless the input contract says they mean the same thing. Also avoid using a property’s truthiness to infer presence, since an owned false or zero value would be missed.

Questions, answered

Can I compare an undeclared name directly with undefined?

The name lookup can throw before comparison. For a genuinely unknown identifier, typeof can perform the safe check shown here, subject to lexical initialization rules.

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.