Javascript Redefine Infinity, NaN, Or "" As 0
I have a javascript calculator which accepts user input, does some math and writes the result to the input field '#theResult' Since '#theResult' updates on keyup, there are times w
Solution 1:
You've used theResult === 0
where you should have used theResult = 0
.
The ===
is for comparisons. When you want to change the value of a variable, you use the assignment operator =
.
Like @zzzzBov said, the simplest thing to do is replace the whole thing with
theResult = isFinite(theResult) && theResult || 0; // updated
That glosses over the subtleties of NaN
however; if the user is finished and the result really is NaN
, showing that as 0
is not really correct.
Post a Comment for "Javascript Redefine Infinity, NaN, Or "" As 0"