0

How to evaluate this statement in javascript?

 isnan = NaN;
 notnan = "hello";
 typeof(notnan) === "number" && isNaN(notnan);

2 Answers2

0

This statment is illogical, because you're trying to see if it's a number and not a number in same statement, making two different booleans output.

typeof(notnan) === "number" && isNaN(notnan);

I believe you're looking for

if (!isNaN(notnan)) {
  // Code if it's a number
} else {
  // Code if it's not a number
}

IF you expect to flip it, you add ! to start it

!isNaN(notnan)
/*
* Returns opposite of isNaN, if it's a number then `true`
* else returns `false`.
*/

EDIT:

 typeof(notnan) === "number" && isNaN(notnan);

Reason this doesn't work is because notnan is a string, therefore it returns false and doesn't evaluate properly. isNaN alone works perfectly fine.

Cernodile
  • 139
  • 7
  • for check a variable (literal) is Nan we must check this variable is typeof number because NaN is number by itself then ckeck this variable is typeof NaN. – Hassan Mohagheghian Aug 08 '17 at 05:27
  • @hassanmohagheghiyan What's the point of checking such thing if isNaN function itself returns either true or false depending on input. You could use parseInt() to ensure it's a number being processed. – Cernodile Aug 08 '17 at 05:30
  • please check typeof(isnan) === NaN that results flase! – Hassan Mohagheghian Aug 08 '17 at 05:37
  • `isnan` isn't a function, and NaN equal to itself is false. To properly detect if it's not a number, please use `isNaN()` function. – Cernodile Aug 08 '17 at 05:39
0

run this it will say something,

isnan = NaN;
notnan = "hello";
if (typeof(notnan) === "number" && isNaN(notnan)){
  console.log('yes');
}
else {
  console.log('typeof(notnan) is :',typeof(notnan), '\n isNaN(notnan) :', isNaN(notnan),'\n this is,',typeof(notnan)=== "number",'-',isNaN(notnan), 'so this condition became false...')
}
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118