---
description: test if a value is a valid number.
---

# isNumber()

[JS](../../../..) ⟩ [value](../../..) ⟩ [primitive](../..) ⟩ [number](..) ⟩ [functions](broken-reference) ⟩ isNumber()

{% hint style="success" %}
determine if a value is a (valid) number, <mark style="color:yellow;">**equivalent**</mark> to [Number.isFinite(x)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite).
{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}
* replit： [isNumber()](https://replit.com/@pegasusroe/isNumber#helpers/isNumber.js)

```javascript
// ⭐️ is (valid) number?
function isNumber(value) {
    return Number.isFinite(value);
}

// export
module.exports = { isNumber };
```

💈範例：

```javascript
value  isNumber()?   Number.isFinite()?
---------------------------------------------
123       ✅         ✅                 
-2.5      ✅         ✅                 
Infinity  ❌         ❌                 
NaN       ❌         ❌                 
'123'     ❌         ❌                 
''        ❌         ❌         empty string
---------------------------------------------
```
{% endtab %}

{% tab title="👥 相關" %}
* :point\_right: [isprimitive](../../../type/type-functions/isprimitive "mention") <mark style="color:yellow;">**/**</mark> [isobject](../../../type/type-functions/isobject "mention")
* :point\_right: [nan](../special/nan "mention")
{% endtab %}

{% tab title="📘 手冊" %}
* [isFinite()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isFinite)
* [Number.isFinite()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite)  ⭐️
{% endtab %}

{% tab title="🗣 討論" %}
* [How to write isNumber() in JavaScript? \[duplicate\]](https://stackoverflow.com/questions/20169217/how-to-write-isnumber-in-javascript)
* [Validate decimal numbers in JavaScript - IsNumeric()](https://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric)
{% endtab %}
{% endtabs %}
