Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is typeof(NaN) in Javascript? [duplicate]

Tags:

javascript

I just happen to stumble upon a code which checks the typeof of a varable passed to it just like this.

function myNaN(b){

  if(typeof(b) == 'number'){
    // execute some code
  }

}

Whenever I call this function it works fine and passes the if condition if number is being passed.

However when I pass a NaN (which is the output of some other function) to this function the if condition returns true.

My question is it correct that typeof(NaN) == 'number' ? If so, why? Isn't it confusing?

Just try running console.log(typeof(NaN)); in browser console to see what I mean.

like image 817
ʞɹᴉʞ ǝʌɐp Avatar asked Jun 07 '26 18:06

ʞɹᴉʞ ǝʌɐp


2 Answers

Yes, typeof(NaN) is number. You can check if the value is NaN specifically using the function isNaN.

like image 63
Guilherme Sehn Avatar answered Jun 09 '26 07:06

Guilherme Sehn


Why don't you use the: "isNan("1234")" function ? Here is some link if it helps: http://www.w3schools.com/jsref/jsref_isnan.asp

like image 21
Hitman Avatar answered Jun 09 '26 06:06

Hitman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!