Why Does JavaScript Return NaN? Understanding NaN and isNaN()

This article explains the special NaN value in ECMAScript, its two unusual properties, how division by zero yields NaN, and demonstrates the behavior of the isNaN() function with various inputs, including numbers, strings, booleans, and objects, illustrating conversion rules and pitfalls.

Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Why Does JavaScript Return NaN? Understanding NaN and isNaN()

NaN (Not a Number) is a special numeric value in ECMAScript used to represent an operation that should produce a number but does not, preventing errors such as division by zero from halting execution.

NaN has two unusual characteristics: any operation involving NaN returns NaN, and NaN is not equal to any value, including itself; for example, NaN == NaN evaluates to false.

The ECMAScript isNaN() function accepts any type of argument and determines whether the value is “not a number.” It first attempts to convert the argument to a number; values that cannot be converted cause isNaN() to return true.

The example tests five different inputs: NaN (returns true), the number 10 and the string “10” (both return false because the string is convertible), the string “blue” (returns true), and the boolean true (converted to 1, returns false).

When isNaN() is called on an object, JavaScript first invokes the object's valueOf() method; if the result cannot be converted to a number, it then calls toString() and tests that result, following the standard execution flow for built‑in functions and operators.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ECMAScriptNaNisNaN
Tencent IMWeb Frontend Team
Written by

Tencent IMWeb Frontend Team

IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.