# Is Number.isInteger inconsistent?

**URL:** <https://es.discourse.group/t/is-number-isinteger-inconsistent/368>\
**Category:** I have questions\
**Created:** [June 19, 2020, 8:07pm UTC](https://es.discourse.group/t/is-number-isinteger-inconsistent/368 "2020-06-19T20:07:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![timreichen](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/timreichen/32/316_2.png) [@timreichen](https://es.discourse.group/u/timreichen)\
**Post date:** [June 19, 2020, 8:07pm UTC](https://es.discourse.group/t/is-number-isinteger-inconsistent/368/1 "2020-06-19T20:07:27Z")

</div>

```javascript
Number.isInteger(1) // output: true

```

```javascript
Number.isInteger(new Number(1)) // output: false

```

but

```javascript
Array.isArray([]) // output: true

```

```javascript
Array.isArray(new Array()) // output: true

```

Is it just me or would `Number.isInteger` be more consistent if `Number.isInteger(new Number(1)) === true`?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [June 19, 2020, 9:14pm UTC](https://es.discourse.group/t/is-number-isinteger-inconsistent/368/2 "2020-06-19T21:14:36Z")

</div>

This isn't inconsistent because `[]` and `new Array()` are identical (just the former is preferred), whereas `1` is a primitive but `new Number(1)` or `Object(1)` are objects. You can compare their `typeof` values as well.

---

<div class="post-metadata">

**Author:** ![timreichen](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/timreichen/32/316_2.png) [@timreichen](https://es.discourse.group/u/timreichen)\
**Post date:** [June 29, 2020, 10:32am UTC](https://es.discourse.group/t/is-number-isinteger-inconsistent/368/3 "2020-06-29T10:32:29Z")

</div>

Yeah but the `Number.isInteger` could also check if the passed value is an instance of Number and then if `value.valueOf()` is an integer.  
`isInteger` does as far as I know not have not to check if it is a primitive number type or not.

And in what case is `Number(1)` not supposed to be an integer?  
If that works

```javascript
const n = new Number(1)
console.log(n + 1) // output: 2

```

then it should be definitely recognized as an integer.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [June 29, 2020, 12:24pm UTC](https://es.discourse.group/t/is-number-isinteger-inconsistent/368/4 "2020-06-29T12:24:32Z")

</div>

If you want to check whether things are integers _after being coerced to a number_, then just write `Number.isInteger(+x)` or `Number.isInteger(Number(x))`. There's no good reason why `isInteger` should implicitly do this on its own. Notice there are lots of values other than `new Number` instances where this would evaluate to true, many of which you would normally not consider to "be an integer".

---

<div class="post-metadata">

**Author:** ![timreichen](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/timreichen/32/316_2.png) [@timreichen](https://es.discourse.group/u/timreichen)\
**Post date:** [June 29, 2020, 5:25pm UTC](https://es.discourse.group/t/is-number-isinteger-inconsistent/368/5 "2020-06-29T17:25:53Z")

</div>

Ok, I understand that. Just out of curiosity: what other values would evaluate to true not being a number primitive or a number instance calling `valueOf()` ?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [June 29, 2020, 5:35pm UTC](https://es.discourse.group/t/is-number-isinteger-inconsistent/368/6 "2020-06-29T17:35:03Z")

</div>

Despite the best practice being to avoid creating boxed primitives, I've written a number of packages for working with them; for example, [https://www.npmjs.com/package/is-number-object](https://www.npmjs.com/package/is-number-object) returns true for both primitive and boxed numbers; [https://www.npmjs.com/package/unbox-primitive](https://www.npmjs.com/package/unbox-primitive) gets a primitive value from a boxed primitive without using `+` (which would coerce non-boxed-numbers to a number as well).

You're right that Number.isInteger _could_ unbox Number primitives and return true for those, but the designed (and now expected) contract for it is that it only returns true for `x` when `typeof x === 'number'` and when x is a finite integer. Changing that now would likely break the web.
