Technical skill practice

JavaScript isNaN: Test a Number Without Accidental Coercion

Number.isNaN checks whether a value is the numeric NaN value. The global isNaN first coerces its argument, so it can answer a different question for text or undefined. Choose deliberately rather than treating the names as equivalent.

The task

Inspect invalid arithmetic, numeric-looking text and an empty string. Check NaN with a dedicated predicate rather than a comparison to itself.

Runnable Node 22 example

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

const broken = 0 / 0;
assert.equal(Number.isNaN(broken), true);
assert.equal(broken === broken, false);
assert.equal(Number.isNaN('oops'), false);
assert.equal(isNaN('oops'), true);
assert.equal(isNaN(''), false);
assert.equal(Number.isNaN(undefined), false);
assert.equal(isNaN(undefined), true);
console.log(`numericNaN=${Number.isNaN(broken)}; textCoerced=${isNaN('oops')}`);

Expected output and explanation

The failed numeric operation produces NaN, and the explicit predicate detects it. Equality with the same value is false, which explains why value === NaN cannot serve as the intended test.

The string oops is not itself the Number NaN, so Number.isNaN returns false. The coercing global operation reports true after trying its numeric conversion. Empty text takes yet another path and is not rejected by that global check. Conversion and validation should therefore remain explicit steps.

Output from the example

numericNaN=true; textCoerced=true

Pitfall to check

Not-NaN does not mean finite or valid for your measurement. Infinity passes a NaN-only test. Use Number.isFinite and any required unit or range checks when that is the actual contract.

Questions, answered

Does Number.isNaN("12") confirm a valid numeric string?

No. It merely says the string is not the numeric NaN value. The separate string-to-number task validates an explicit input rule.

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.