0

I am trying to understand the structure of the javaScript language.

Can someone please tell me:

I understand that isNaN() is a method on the Number constructor.

So, how is it that the following two examples are able to work?

console.log(isNaN(3));

or

if(isNaN(3)) {
    console.log(`3 is *not* a number`);
} else {
    console.log(`3 is a number`);
}

There is no isNaN() function on the window --> i.e. window.isNaN() does not exist.

And you are not writing 3.isNaN() or Number.isNaN(3)

How is it that just writing the isNaN() function (or any other Number method) on its own, you are able to access the Number constructor?

For contrast:

When you implement a String method, you dot it off of an actual string, so the String methods are inherited by this string method. Example:

let littleString = 'I am a string'.toLowerCase();

You can't write:

toLowerCase('I am a little string');

or you will get an error:

ReferenceError: Can't find variable: toLowerCase

So, why can you do this with numbers?

Thanks!

Maiya
  • 932
  • 2
  • 10
  • 26
  • 2
    `Number.isNaN` is a fixed version of the global `isNaN` because the latter is broken but has to remain broken for backwards-compatibility. Although it causes confusion, it was an acceptable compromise to attach the fixed version to `Number`. – Jared Smith Feb 16 '18 at 19:01
  • Thank you @Jared Smith. That helps a lot. – Maiya Feb 17 '18 at 00:53

2 Answers2

3

I understand that isNaN() is a method on the Number constructor, and there is no isNaN() function on the window

No. There are in fact two isNaN functions (and they even work differently):

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 3
    So actually, "Yes, but..." ;-) – trincot Feb 16 '18 at 18:56
  • Thanks @Bergi. When I console.log(window) in Chrome, it doesn't show the isNaN() method. So, it's confusing! Thanks for answering the question! – Maiya Feb 16 '18 at 19:05
  • 2
    @Maiya That's not how you should check if an object has a property. What you should do, is `"isNaN" in window`, which returns true. – GregRos Feb 16 '18 at 19:08
  • 3
    @Maiya The properties of the `window` object are a bit confusing. It might be inherited, it might be non-enumerable, it might not be shown directly in the console view for any other reason. Have a look at https://stackoverflow.com/q/10374462/1048572 or https://stackoverflow.com/q/17246309/1048572 – Bergi Feb 16 '18 at 19:10
2

The global functions — functions which are called globally rather than on an object—directly return their results to the caller.

isNaN is a global function but toLowerCase is not.

See the global functions:

eval()
uneval() // not standardized
isFinite()
isNaN()
parseFloat()
parseInt()
decodeURI()
decodeURIComponent()
encodeURI()
encodeURIComponent()
escape() // deprecated
unescape() // deprecated

Since, toLowerCase() is a prototype of String constructor, you need to call this method on string.

In fact, isNaN is defined globally (inherited from window object) and also in Number constructor.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231