# Silly JavaScript question

**URL:** <https://discourse.nodered.org/t/silly-javascript-question/77223>\
**Category:** General\
**Created:** [3 April 2023 23:51 UTC](https://discourse.nodered.org/t/silly-javascript-question/77223 "2023-04-03T23:51:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [3 April 2023 23:51 UTC](https://discourse.nodered.org/t/silly-javascript-question/77223/1 "2023-04-03T23:51:36Z")

</div>

I realize this isn't strictly a Node-RED question, but it came up in designing a validation function for a custom node. I want to test whether a text input represents an integer. I get these results:

```auto
let v = '3'
node.warn(Number.isInteger(v)) // false
node.warn(Number.isInteger(Number.parseFloat(v))) // true

```

Is there a less silly-looking way to write the third line?

[EDIT] OK, I just realized I can get away with `Number.isInteger(Number(v))`. Still, for a language that is so careless with type conversion, I expected less...

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [4 April 2023 00:14 UTC](https://discourse.nodered.org/t/silly-javascript-question/77223/2 "2023-04-04T00:14:57Z")

</div>

`Number(v) % 1 === 0` should also work, you could omit the Number() as JS will force a number when using modulus, but you would need to suppress the editor error.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [4 April 2023 00:26 UTC](https://discourse.nodered.org/t/silly-javascript-question/77223/3 "2023-04-04T00:26:21Z")

</div>

Nice. Suggests `Number.isInteger(-v)`.

Actually, I want a positive integer, so there is a bit more work to do.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [6 April 2023 10:14 UTC](https://discourse.nodered.org/t/silly-javascript-question/77223/4 "2023-04-06T10:14:14Z")

</div>

> [@drmibell](#):
>
> Actually, I want a positive integer, so there is a bit more work to do.

`Number.isInteger(+v)`

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 April 2023 10:19 UTC](https://discourse.nodered.org/t/silly-javascript-question/77223/5 "2023-04-06T10:19:21Z")

</div>

`Number.isInteger(+v)` will not verify the string `v` is a positive integer

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b56f69f6b64a19a4debbd4a0cd83bbcdd079de17.png)

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [6 April 2023 13:52 UTC](https://discourse.nodered.org/t/silly-javascript-question/77223/6 "2023-04-06T13:52:52Z")

</div>

That's right. So far, the least ugly is

```auto
+v > 0 && Number.isInteger(+v)

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 June 2023 13:53 UTC](https://discourse.nodered.org/t/silly-javascript-question/77223/7 "2023-06-05T13:53:29Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
