Convert Nan to 0 in JavaScript

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 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

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



Leave a reply



Submit