1

Let's say i want to convert a string that obviously is not a number, but occurs that at final i get a number!?

var b = parseFloat("a");

console.log(b); // output NaN

console.log(typeof b); // print number!

What exactly is happening here? NaN means for not a number, right?

Ok, so NaN == NaN is always false. But:

typeof 1/0 // prints NaN
1/0 == 1/0 // its true
Savrige
  • 3,352
  • 3
  • 32
  • 38
  • The case with `typeof null === 'object'` is the same as `typeof NaN === 'number'`. – Dan D. Oct 03 '14 at 03:46
  • 2
    http://stackoverflow.com/a/6678664/ – stewe Oct 03 '14 at 03:51
  • 2
    Regarding your edit, `typeof 1/0` is evaluated as `(typeof 1)/0` which is `"number"/0`. JS will try to convert the string to a number, which results in `NaN`, so you are doing `NaN/0` which will of course result in `NaN`. Even without knowing the operator presedence, it's clear that `typeof 1` is executed first be use a) `typeof` always returns a string, and `NaN` is not a string, b) `NaN` is a value, not a type, c) `typeof` *never* returns `NaN`. `1/0` returns Infinity. – Felix Kling Oct 03 '14 at 05:04

3 Answers3

1

It's not really specific to Javascript. Its more related to computer science.

In computing, NaN, standing for not a number, is a numeric data type value representing an undefined or unrepresentable value, especially in floating-point calculations.

There are three kinds of operations that can return NaN

Operations with a NaN as at least one operand.
Indeterminate forms
    The divisions 0/0 and ±∞/±∞
    The multiplications 0×±∞ and ±∞×0
    The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions
    The standard has alternative functions for powers:
        The standard pow function and the integer exponent pown function define 00, 1∞, and ∞0 as 1.
        The powr function defines all three indeterminate forms as invalid operations and so returns NaN.

Real operations with complex results, for example:
    The square root of a negative number.
    The logarithm of a negative number
    The inverse sine or cosine of a number that is less than −1 or greater than +1.
Prasanth Louis
  • 4,658
  • 2
  • 34
  • 47
1
var b = parseFloat("a");

console.log(b); // output NaN

console.log(typeof b);  // number! what else would it be?
console.log(!isNaN(b)); // false 
console.log(isNaN(b));  // true!

console.log(isNaN('stringoftext'));  // true! this is not good, that's REALLY not a number
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

console.log(typeof b === 'number' && isNaN(b) === true); // It is NOT A NUMBER

console.log(b != b); // true , means that it is NaN
//http://stackoverflow.com/a/14220688/3741423
tbh__
  • 324
  • 3
  • 17
  • console.log(b == b) ??? false!? Can you explain this behaviour? – Savrige Oct 03 '14 at 03:55
  • 1
    @Borachio: NaN is a special value that is not equals to anything. Not even itself: `NaN == NaN` is false. – slebetman Oct 03 '14 at 03:59
  • 1
    I posted another thread that talks about it http://stackoverflow.com/a/14220688/3741423 I also fix, it is true not false, thanks slebetman – tbh__ Oct 03 '14 at 04:00
  • I love this guy's explanation as well: http://stackoverflow.com/a/23666623/3741423 – tbh__ Oct 03 '14 at 04:10
  • @tbh__ checkout this: typeof 1/0 // prints NaN and 1/0 == 1/0 prints true – Savrige Oct 03 '14 at 04:13
  • You can't divide one by zero it ends up being infinity, you could do it forever... NaN because you can't use that in an equation. Infinity isn't literally NaN though if you console.log(1/0); it prints 'infinity'; – tbh__ Oct 03 '14 at 04:19
  • That is interesting about the typeof being NaN though, when the value isn't. http://www.2ality.com/2012/02/nan-infinity.html – tbh__ Oct 03 '14 at 04:21
  • @Borachio No, that's an operator precedence issue. Try `typeof (1/0)`. instead. – Qantas 94 Heavy Oct 03 '14 at 04:24
  • ahhh good point, so it was getting number from typeof 1 then trying to divide that by 0? – tbh__ Oct 03 '14 at 04:27
  • @Qantas94Heavy I dont think so. Try typeof a/2 --> this should return "string" in your logic, but return NaN. – Savrige Oct 03 '14 at 04:34
  • 1
    a = 'a'; console.log(typeof a);// == string console.log(a / 2); // == NaN console.log(typeof a/2); // == NaN console.log(typeof (a/2));// == Number – tbh__ Oct 03 '14 at 04:39
  • assuming that a is a string variable... unless you meant to say 'a' even then typeof 'a' would equal string, but then get divided by 2, resulting in NaN being returned. If you wrap it in () then it divids a by 2, it gets NaN and then runs typeof on it and gets 'number' – tbh__ Oct 03 '14 at 04:41
0

Threre is no explanation you want to see. Funciton parseFloat could raise an exception, but it's another aproach. It returns special "number" called "not a number". Just don't go in cycles with the name.

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79