devxlogo

Checking for Value “NaN”

Checking for Value “NaN”

Question:
Why does my if statement

if (final == "NaN") {  window.alert("You...are...a...dunce.  Hit reload to try again.");}

fail to detect when final is “NaN”? It always goes straight on to the else part, and then comes back with the answer “Required Percentage: NaN%.”

Answer:
If you put “NaN” in quotes like that, it thinks that you’re trying to compare final to a string. You can’t compare anything to Nan directly; instead, you need to use the built-in isNan() function.

What you want to do is instead say:

if (isNan(final)) {...

See also  Redefining Energy Management: Advanced Solutions for Modern Buildings
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist