Convert NaN to 0 in JavaScript
You can do this:
a = a || 0
...which will convert a from any "falsey" value to 0
.
The "falsey" values are:
false
null
undefined
0
""
( empty string )NaN
( Not a Number )
Or this if you prefer:
a = a ? a : 0;
...which will have the same effect as above.
If the intent was to test for more than just NaN
, then you can do the same, but do a toNumber conversion first.
a = +a || 0
This uses the unary + operator to try to convert a
to a number. This has the added benefit of converting things like numeric strings '123'
to a number.
The only unexpected thing may be if someone passes an Array that can successfully be converted to a number:
+['123'] // 123
Here we have an Array that has a single member that is a numeric string. It will be successfully converted to a number.
Change NaN to 0 in Javascript
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 toNaN
, but aren't actually the same value asNaN
. This also means that only values of the typenumber
, that are alsoNaN
, returntrue
.
Compare:
isNaN("a"); // true
Number.isNaN("a"); // false
How to convert NaN value to 0 inside app.post in .js file
you can do this:
let total1 = (parseInt(question1) || 0) + (parseInt(question2) || 0)
this will convert any falsy value to 0
How to remove or simply substitute Nan with 0?
Change this line:
var n1 = parseFloat(document.getElementById('n1').value);
to this one:
var n1 = parseFloat(document.getElementById('n1').value) || 0;
This will give n1
a value of 0 if you leave its input field blank.
How to turn NaN from parseInt into 0 for an empty string?
var s = '';
var num = parseInt(s) || 0;
When not used with boolean values, the logical OR ||
operator returns the first expression parseInt(s)
if it can be evaluated to true
, otherwise it returns the second expression 0
. The return value of parseInt('')
is NaN
. NaN
evaluates to false
, so num
ends up being set to 0
.
Replace NaN with 0 in my innerHTML
Assuming that decimal values are allowed, lose the eval
calls!
var one = +document.theForm.elements[0].value || 0;
The +
sign will attempt to convert the value to a number, and evaluates to NaN
if the value is not a legal number. The || 0
will convert NaN
to zero, if found.
If only whole numbers are permitted, use parseInt
instead of +
, and always supply the radix parameter to ensure that leading zeroes don't lead to an inadvertent parsing as octal:
var one = parseInt(document.theForm.elements[0].value, 10) || 0;
Related Topics
Ajax Call and Clean JSON But Syntax Error: Missing ; Before Statement
Firing a Keyboard Event on Chrome
Javascript/Jquery: Set Values (Selection) in a Multiple Select
Trying to Use the Domparser with Node Js
Detect If Browser Tab Is Active or User Has Switched Away
Background-Color Hex to JavaScript Variable
Node.Js Async/Await Using with MySQL
Watch for Object Properties Changes in JavaScript
Can Any Desktop Browsers Detect When the Computer Resumes from Sleep
How to Format a Date in Mm/Dd/Yyyy Hh:Mm:Ss Format in JavaScript
How Dangerous Is It in JavaScript, Really, to Assume Undefined Is Not Overwritten
How to Replace the Entire HTML Node Using Jquery
What's a Good Tool to Screen-Scrape with JavaScript Support
Google Maps Move Marker with Lat/Lng from Ajax Success Returned Data