Break on NaN in 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");
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.)
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
.
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.