05 / 06

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

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

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.

Scenario Questions

0-2 years experience

  1. 1How would you check that a value entered in a form field is a number before you add it to a total?
  2. 2What does isNaN('123') return and why?
  3. 3If you call isNaN(undefined), what result do you get and what does it mean?

2-5 years experience

  1. 1We 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. 2Why might using isNaN on a string like '0' cause unexpected validation behavior in a feature you’re building?
  3. 3Explain the difference between isNaN and Number.isNaN and when you’d prefer one over the other in production code.

5-8 years experience

  1. 1Our 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. 2Design a reusable validation utility that safely distinguishes numeric from non‑numeric values, handling edge cases such as empty strings, null, and objects.
  3. 3How would you refactor a legacy codebase that heavily relies on isNaN to improve reliability without breaking existing behavior?

8+ years experience

  1. 1At 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. 2Discuss 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. 3How 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?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.