05 / 06

What is the purpose of the `isNaN()` function?

The isNaN() function is used to determine whether a value is NaN (Not-a-Number) or not. It returns true if the value is not a valid number; otherwise, it returns false.

javascript

isNaN() doesn't just check if a value is currently NaN; it checks if the value cannot be converted into a number. This leads to some very famous JavaScript quirks.

Difficulty: 3/10
Topics: type coercion, validation, numeric parsing

Scenario Questions

0-2 years experience
  1. 1

    How would you check that a value entered in a form field is a number before you add it to a total?

  2. 2

    What does isNaN('123') return and why?

  3. 3

    If you call isNaN(undefined), what result do you get and what does it mean?

2-5 years experience
  1. 1

    We have a function that sometimes throws NaN errors when processing user input. Walk me through how you'd use isNaN to debug the problem.

  2. 2

    Why might using isNaN on a string like '0' cause unexpected validation behavior in a feature you’re building?

  3. 3

    Explain the difference between isNaN and Number.isNaN and when you’d prefer one over the other in production code.

5-8 years experience
  1. 1

    Our analytics pipeline processes millions of numeric strings. Discuss the correctness and performance implications of using the global isNaN versus Number.isNaN across the codebase.

  2. 2

    Design a reusable validation utility that safely distinguishes numeric from non‑numeric values, handling edge cases such as empty strings, null, and objects.

  3. 3

    How would you refactor a legacy codebase that heavily relies on isNaN to improve reliability without breaking existing behavior?

8+ years experience
  1. 1

    At a platform level we need strict numeric validation across many services written in JavaScript/TypeScript. What architectural changes would you propose to replace isNaN usage, ensure type safety, and keep backward compatibility?

  2. 2

    Discuss the trade‑offs of introducing a lint rule or compiler plugin that bans the global isNaN in favor of Number.isNaN, including impact on developer workflow and CI pipelines.

  3. 3

    How would you plan a migration strategy for deprecating isNaN in a large monorepo, balancing risk, testing coverage, and documentation updates?

Follow-up Questions

  • Can you show a case where isNaN returns true for a value you expected to be numeric?
  • How does JavaScript's type coercion affect the result of isNaN?
  • What modern alternative would you use instead of the global isNaN?