Break on NaN in JavaScript

JavascriptNan

Javascript Problem Overview


Is there any modern browser that raises exceptions on NaN propagation (ie multiplying or adding a number to NaN), or that can be configured to do so?

Silent NaN propagation is an awful and insidious source of bugs, and I'd love to be able to detect them early, even at a performance penalty.


Here's an example bug that use strict, jshint et al. wouldn't pick up:

object = new MyObject();
object.position.x = 0;
object.position.y = 10;

// ... lots of code

var newPosition = object.position + 1; // <- this is an error, and should
                                       //    have been object.position.x
                                       //    however it fails *silently*,
                                       //    rather than loudly

newPosition *= 2;                      // <- this doesn't raise any errors either.
                                       //    this code is actually ok if the
                                       //    previous line had been correct

Note: The TypeScript compiler is able to detect errors like the above, even in JS code, if type inference succeeds.

Javascript Solutions


Solution 1 - Javascript

To answer the question as asked:

> Is there any modern browser that raises exceptions on NaN propagation (ie multiplying or adding a number to NaN), or that can be configured to do so?

No. Javascript is a very forgiving language and doesn't care if you want to multiply Math.PI by 'potato' (hint: it's NaN). It's just one of the bad parts (or good parts, depending on your perspective) about the language that us developers have to deal with.

Addressing the bug that has you asking this question (presumably), using getters and setters on your Objects is one solid way to enforce this and also keep you from making mistakes like this.

Solution 2 - Javascript

Code below might help you.

To solve this problem fully, I think we need something like operator reload. We can reload operators like '+ - / *', and check if the operand is number, if not, then throw Error.

As a partial solution, when JavaScript does an operation like 'a + b', it will call the valueOf method which inherits from Object.prototype, we can rewrite Object.prototype.valueOf.

Object.prototype.originalValueOf = Object.prototype.valueOf;

Object.prototype.valueOf = function() {
  if (typeof this !== 'number') {
    throw new Error('Object is not a Number');
  }

  return this.originalValueOf();
}

var a = 1 + 2; // -> works
console.log(a); // -> 3

var b = {};
var c = b + 2; // -> will throw an Error

(hint: You can remove the code in production, and add it into your developing environment.)

Solution 3 - Javascript

the cleanest way to do that is having a short handy function that validates the expression's result every time

i know that's not the answer you are looking for but this is the javascript's nature and you can't change it sadly

function v(a){ if(isNaN(a)) throw "error"; return a; }
var test = v(100 * "abc");

Solution 4 - Javascript

I know this is an old thread but I think there may be a super simple answer to your question. You can either create a function for the below statement or add it inline.

JavaScript has some unique behaviors for certain values. One of these unique behaviors revolves around the not-a-number value or NaN. The value of NaN will not compare equal to any other value including NaN itself. For this reason the following statement:

if(x != x) {
    throw "Value is NaN!";
}

will hold true if and only if the value of x is NaN.

Solution 5 - Javascript

I think this is a situation where getter and setter comes in handy.

Following is a psuedo code example just to give you an idea.

//Inside your Object class code.
function getPosition() {

  //You don't want the property "position" to be NaN, right?
  if(isNaN(this.position)) 
    throws "NaN is not a correct numerical value!";

  return this.position;

}


//Somewhere in your code
var newPosition = object.getPosition() + 1; //raises an exception.
  

I think this is better than implementing fake operator overloading and make things more complicated.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkibibuView Question on Stackoverflow
Solution 1 - JavascriptRob M.View Answer on Stackoverflow
Solution 2 - JavascriptJerryView Answer on Stackoverflow
Solution 3 - Javascriptuser652649View Answer on Stackoverflow
Solution 4 - JavascriptWar10ckView Answer on Stackoverflow
Solution 5 - JavascriptJasonView Answer on Stackoverflow