-1

It's JavaScript Code (used Console) and have a small doubt on this code

var foo = 10;
var boo = undefined;

if((foo + boo)==NaN){console.log("Not a Number !!!");}
//>undefined
if((foo + boo)===NaN){console.log("Not a Number !!!");}
//>undefined

//>Not a Number !!!    (Expected)

2 Answers2

2

Equality operator (== and ===) cannot be used to test a value against NaN. Use Number.isNaN() or isNaN() instead.

NaN === NaN;        // false
Number.NaN === NaN; // false
isNaN(NaN);         // true
isNaN(Number.NaN);  // true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN

Andrew
  • 5,290
  • 1
  • 19
  • 22
1

See below URL to fix your Issue....

JavaScript isNaN() Function

Check whether a number is an illegal number:

var a = isNaN(123) + "<br>";
var b = isNaN(-1.23) + "<br>";
var c = isNaN(5-2) + "<br>";
var d = isNaN(0) + "<br>";
var e = isNaN("Hello") + "<br>";
var f = isNaN("2005/12/12") + "<br>";

The result of res will be:

 false
 false
 false
 false
 true
 true 
Vishal Patel
  • 953
  • 5
  • 11
  • 1
    This isn't an answer. This is just a link and if the link dies, this post is useless. Answers should be self contained. Please include the actual information in your post. Linking to external content *in addition* is perfectly fine. Aside from that, the information given at w3schools is not correct (oh wonder!) or at least confusing: `isNaN` doesn't check whether a value is an "illegal number" (whatever that is), it checks whether the value is `NaN`, which actually is a very special number value. – Felix Kling Dec 05 '13 at 07:32
  • @FelixKling Thank you for your kind of suggestion... – Vishal Patel Dec 05 '13 at 07:36
  • You should get into the habit to refer to the MDN documentation (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN), rather than w3schools. For why, see http://www.w3fools.com/. – Felix Kling Dec 05 '13 at 07:38