0

How to evaluate this statement in javascript?

 isnan = NaN;
 notnan = "hello";
 typeof(notnan) === "number" && isNaN(notnan);
hassan
  • 51
  • 10
  • 1
    What is expected result? If the first part of the expression is `true` how could the second part of expression also be `true`? – guest271314 Aug 08 '17 at 05:14
  • in right side: two operands are evaluated by and operator then equality checked or equality checked with any operands in the right side then evaluate both results? – hassan Aug 08 '17 at 05:23
  • 1
    How can the value be a number and not be a number at the same time? As far as am aware of quantum computing is not implemented at JavaScript at this time – guest271314 Aug 08 '17 at 05:25
  • Um whys NaN not working? Its typeof Number but not a number ... :/ – Jonas Wilms Aug 08 '17 at 05:28
  • please check this statement: typeof(isnan) === NaN ; isnan is nan but results false. – hassan Aug 08 '17 at 05:34
  • Because `isNaN` is a function. `NaN` is not a string. `NaN` is not a JavaScript type – guest271314 Aug 08 '17 at 05:36
  • He's trying to see if a string is a number in `typeof(notnan) === "number"`, I believe that is the issue. – Cernodile Aug 08 '17 at 05:37
  • If you are expecting a `true` result for the expression you can use `!` operator `!(typeof(notnan) === "number") /* notnan is not a "number" : false, which we invert to true */ && isNaN(notnan) // again, notnan is not a number, true && true === true` – guest271314 Aug 08 '17 at 05:39
  • this post solved by another post. – hassan Aug 08 '17 at 05:54

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
  • 129
  • 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 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 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
  • 16,635
  • 8
  • 97
  • 110