0

Is there any way to detect - or safely guard against - NaN values during runtime, across any part of the application where these might appear?

  • A) Is there a safe linting to warn about potential NaN values that might occur in certain constructs?
  • B) Alternatively, some kind of solution that monitors the executed JavaScript during runtime, detects and logs or alerts about it? - Similar to how error detection services work?

Example

Pseudo-code of a scenario where testing for NaN might be hard:

function undetectedNaN (): AssumeStrictDataTypes {
  // 1. Some algorithm ...
  // 2. NaN appears undetected ...
  // 3. Some algorithm ...
  // 4. Return: Broken but seeming legit value and data type
}

(Assume that it's in a larger application codebase, where adding checks for isNaN() and validating individual values is not an option: It might be too costly, not a requirement, no automated testing possible etc. ...)


UPDATE 2021/12: To complement my rather abstract question: In a more practical use case, API values did not exist in an array structure. Most occurrences of NaN could be solved by better value/type checking.

function example (a, b) {
  return a[0] + b[0]
}

// undefined + undefined = NaN
const result = example([], []);

console.log(result);

Some other possibilities of NaN occuring:

const testArray  = [];
const testObject = {};

testArray[0] + testArray[0] // NaN
testObject.X + testObject.X // NaN
undefined + undefined       // NaN
NaN + NaN                   // NaN

Other places where NaN might be harder to track: e.g. if it appears during runtime, somewhere, sometimes, and is not a testable return value.

// Bitwise operators
~(NaN) // -1

// Conditionals
if (NaN > 1) {} // false

// Typecasting
!!NaN // false
TheRemoteCoder
  • 172
  • 2
  • 12

1 Answers1

1

There is no simple way to achieve this.

There is no easy way of detecting this statically (for a linter). NaN can be a result from simple operations like multiplication and division, so it could occur anywhere.

Of course it would be technically possible to detect expressions that always evaluate to NaN, such as if you were to type 0/0. But this is probably not what you're looking for.

To my knowledge there is no framework that can detect NaN occuring somewhere at runtime. I believe that to achieve this in the way that you want, you would need support for it at JS engine level.

You could invent hacky solutions like creating functions for arithmetic operations like multiply or wrap numbers in objects that override the valueOf() function. But none of them will work in every case and they make your code a lot less pleasant.

In the end NaN is a perfectly valid number. You would detect it like you would detect any other value. For example, if your algorithm couldn't handle negative numbers, you also couldn't bend the language so it tells you when you have negative numbers. Instead you would have to design your code in such a way that negative values don't occur or you write a lot of checks/assertions.

Burak
  • 667
  • 1
  • 11
  • 19