Change NaN to 0 in Javascript
javascript
Solution
You can check whether a value is `NaN` using the `isNaN` function:
var result = average();
if (isNaN(result)) result = 0;
In the future, when ECMAScript 6 is widely available one may consider switching to `Number.isNaN`, as `isNaN` does not properly handle strings as parameters.
In comparison to the global `isNaN` function, `Number.isNaN` doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to `NaN`, but aren't actually the same value as `NaN`. This also means that only values of the type `number`, that are also `NaN`, return `true`.
Compare:
isNaN("a"); // true
Number.isNaN("a"); // false
Problem
I have a code that return to me result of javascript average function in bootstrap modal. But when one of inputs are blank, he return NaN . How i can change NaN to 0? Here is my code that return result: ``` $(".average").click(function () { var result = average(); $("#myModal .modal-body h2").text(result); $("#myModal").modal(); }); ```