Duplicate question, removed for future people
Asked
Active
Viewed 250 times
-1
-
1Nothing equals `NaN`. Even `NaN === NaN; //false` – Yury Tarabanko Aug 19 '16 at 17:07
-
Well, you would need to use [`isNaN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN) and you can't assign to `this`. `this = anything` is invalid. – Mike Cluck Aug 19 '16 at 17:07
-
You need to look at all the variables, there is no "everything" – epascarello Aug 19 '16 at 17:07
-
As a shorthand you may be able to use `(varname || 0)`, as this will (among other things) keep numeric values as they are but replace `NaN` (falsy value) with `0`. – Niet the Dark Absol Aug 19 '16 at 17:08
-
Hey Yury, that was a loose example to get the point across of the intention as I said. You could substitute: isNan(*anythingHere) == true – D. Cantatore Aug 20 '16 at 19:24
3 Answers
0
I don't think so, since as far as I know nothing is equal to NaN. However, you can take advantage of that and do:
if( * !== *){
//code when * is NaN
};
And only a NaN will meet the condition. I read this is safer than just to use isNaN() .
Sergeon
- 6,218
- 1
- 19
- 41
0
You can do something like this
function convertIfNaN (x) {
if (isNaN(x))
return 0;
else
return x;
}
yitzih
- 2,850
- 3
- 26
- 42
0
You can use the isNaN function like so:
if (isNaN(x)) {
// do something
}
The built in isNaN function will only check if the value can be coerced to a number. You can also write your own function to simply check for the value "NaN" like so:
function isNaN(x){
return x !== x;
}
dalyhabit
- 19
- 4
-
I think this is the closest to what I was looking to achieve. It appears the simple clever code option doesn't really fit the situation and I just did a copy paste of a small if statement to check each time the variable would be used before it was used. Thanks! – D. Cantatore Aug 20 '16 at 19:28