Why can't I use `.isInteger()` on a number itself?

Why is “console.log(Number.isInteger(2017));” used for determining whether 2017 is a whole number, and not "console.log(2017.isInteger()) ?

3 Likes

I think its because your calling the function or whatever, or what you want to do with it, first. Its kinda like how yoda talks. “to the garbage, take this item” or something like that. so instead of is 2017 an integer, its “is an integer, 2017?”…I think

1 Like

When you’re using a method on an object (in this case the isInteger method on the number object)- Do I always have to refer to the general Object first? (Here: Number.) As in, does it apply to methods on other objects? Also I’m wondering why this doesn’t work (without “Number”): console.log(isInteger(2017)) …?

This explanation may not be entirely correct. In case of String examples JS syntax is ‘my string’.toUpperCase() and not String.toUpperCase(‘my string’)

I hope someone can answer andreiradulescu question above.

Number.isInteger(n) is a Class Method, not an instance method, so is not inherited by numbers. We must address the class directly, and pass in an argument for the method to test.

Compare this to, Array.isArray(sequence), which is also a class method not inherited by array objects.

That is unlike, String.prototype.toLowerCase() which is an instance method inherited by all string objects.

myString.toLowerCase()

Notice there is no argument since the method acts upon the object in context.

2 Likes