# isClass()

[JS](../../..) ⟩ [values](../..) ⟩ [custom functions]() ⟩ isClass()

{% hint style="success" %}
check if value is a [class](../../class "mention"). (Note：class is also a [func](../../builtin/func "mention"))
{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}
* replit：[typeName() (v.2)](https://replit.com/@pegasusroe/typeName-from-MDN#index.js)
* require： [isfunction](isfunction "mention"), [func-def](func-def "mention")
* (to use it, copy and paste from 📁 module file： [typename](../name/typename "mention"))

```javascript
// ⭐️ check if value is a "class"
// - require: 
//   • isFunction()
//   • functionDefinition()
function isClass(value) {
    return (
        // `value` is a function
        isFunction(value) &&
        // `value` is a "class" definition
        functionDefinition(value).startsWith("class")
    );
}
```
{% endtab %}

{% tab title="💈範例" %}
* replit：[isClass()](https://replit.com/@pegasusroe/isClass#index.js)
* require： [isfunction](isfunction "mention"), [func-def](func-def "mention")

```javascript
// normal function
function foo() {
    console.log('hello');
}

let a = 1;               // number
class User {}            // class
let user = new User();   // instance

// test cases
isClass(foo),    // ❌ false
isClass(a),      // ❌ false

isClass(User),   // ✅ true
isClass(user),   // ❌ false

// built-in objects
isClass(Object),        // ❌ false❗ 
isClass(Array),         // ❌ false❗ 
isClass(Promise),       // ❌ false❗
isClass(Proxy),         // ❌ false❗

isFunction(Object),     // ✅ true
isFunction(Array),      // ✅ true

// functionDefinition(a),   // ⛔ TypeError

functionDefinition(foo),    // function foo() {
                            //     console.log('hello');
                            // }
functionDefinition(Array),
// function Array() { [native code] }

functionDefinition(Promise),
// function Promise() { [native code] }
```
{% endtab %}

{% tab title="⬇️ 應用" %}
* [typename](../name/typename "mention")
{% endtab %}

{% tab title="🗣 討論" %}
* [How to check if a variable is an ES6 class declaration?](https://stackoverflow.com/q/30758961/5409815) ([my answer](https://stackoverflow.com/a/72326559/5409815))
{% endtab %}
{% endtabs %}
