# 1.2.6.2.「typeof」、「instanceof」與搞清楚這是什麼

* typeof
  * 告訴你型別是甚麼

    ```
    var a = 3;
    //印出Number
    console.log(typeof a);

    var d = [];
    //印出Object
    console.log(typeof d);
    ```
* 用陣列在[原型鍊](https://jenhsuan.gitbooks.io/javascript-node-js/content/chapter1/124javascript-de-wu-jian-dao-xiang-yu-yuan-xing-ji-cheng/1242liao-jie-yuan-xing.html)上呼叫toString
  * [call](https://jenhsuan.gitbooks.io/javascript-node-js/content/chapter1/12321callapply-yu-bind.html): 將this重新指向d並傳入其他參數, 並執行函數
  * toString會用this變數輸出他的值

    ```
    //印出[object Array]
    console.log(Object.prototype.toString.call(d));
    ```
* instanceof
  * 告訴你型別是甚麼

    ```
    var e = new Person('Jane');
    //true
    console.log(a instanceof Person);
    ```
* undefined, null
  * bug: typeof null是object

    ```
    //印出undefined
    console.log(typeof undefined);
    //印出object
    console.log(typeof null);
    ```
* empty function

  ```
    var z = function(){}
    //印出function
    console.log(typeof z);
  ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/chapter1/12javascript-quan-gong-lve-ff1a-ke-fu-js-de-qi-guai-bu-fen/126za-tan/1262typeofinstanceof300d-yu-gao-qing-chu-zhe-shi-shi-me.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
