Number isNaN Refuses String Coercion
Number.isNaN and Number.isFinite skip the numeric coercion the global isNaN and isFinite apply, so only the real NaN value and real numbers pass.
Code
// 全局 isNaN 先把参数做强转(coercion)再判断:"text" 转成 NaN,返回 true
console.log('isNaN("text"):', isNaN('text'));
// Number.isNaN 不做转换:只有真正的 NaN 值才返回 true
console.log('Number.isNaN("text"):', Number.isNaN('text'));
const parsed = Number.parseInt('n/a', 10);
console.log('parsed:', parsed);
console.log('Number.isNaN(parsed):', Number.isNaN(parsed));
// Number.isFinite 同样不转换;普通 isFinite 会把 "3" 当成数字
console.log('isFinite("3"):', isFinite('3'));
console.log('Number.isFinite("3"):', Number.isFinite('3'));
console.log('Number.isFinite(Infinity):', Number.isFinite(Infinity));Output
Captured from a sandboxed run of this snippet in the build pipeline.
isNaN("text"): true
Number.isNaN("text"): false
parsed: NaN
Number.isNaN(parsed): true
isFinite("3"): true
Number.isFinite("3"): false
Number.isFinite(Infinity): falseHow the snippet works
Top-level bindings parsed hold the demo state — nothing sits inside a function scope.
Using it
The run emits 7 labeled sections — isNaN(, Number.isNaN(, parsed:, Number.isNaN(parsed):, isFinite(, Number.isFinite(, Number.isFinite(Infinity): — one per step.
Key statements
const parsed = Number.parseInt('n/a', 10);
Interactive Tools for This Topic
Number to WordsConvert an integer up to 999,999,999,999 into US English words, using the short scale and a minus prefix for negative values.Number Base ConverterConvert arbitrary-size integers between binary, octal, decimal, hexadecimal, and base 36 with digit validation and signed input support.Scientific Notation ConverterConvert numbers between plain decimal and scientific notation, optionally fixing the significant digits and splitting out the mantissa and exponent.A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.
FAQ
- Does this work in a bare project with no dependencies file?
- Yes — a bare project runs it as-is, since nothing needs declaring. Its state lives in module-level bindings: `parsed`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.