# object -> primitive conversion

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [value](https://lochiwei.gitbook.io/web/js/val) ⟩ [type](https://lochiwei.gitbook.io/web/js/val/type) ⟩ [conversion](https://lochiwei.gitbook.io/web/js/val/type/convert) ⟩ object -> primitive

{% hint style="success" %}
[js-spec](https://lochiwei.gitbook.io/web/master/ref/js-spec "mention") defines <mark style="color:red;">**3**</mark> <mark style="color:yellow;">**algorithms**</mark> for <mark style="color:red;">**object**</mark>**&#x20;**<mark style="color:yellow;">**-> primitive**</mark> <mark style="color:purple;">**conversion**</mark>：

* [<mark style="color:yellow;">**prefer-**</mark><mark style="color:red;">**string**</mark>](https://lochiwei.gitbook.io/web/js/val/obj/convert/obj-to-prim/prefer-string)：preferring a string (if conversion is possible)
* [<mark style="color:yellow;">**prefer-**</mark><mark style="color:green;">**number**</mark>](https://lochiwei.gitbook.io/web/js/val/obj/convert/obj-to-prim/prefer-number)：preferring a number (if conversion is possible)
* [**no-preference**](https://lochiwei.gitbook.io/web/js/val/obj/convert/obj-to-prim/no-preference)：classes choose their own preference.&#x20;
  {% endhint %}

{% tabs %}
{% tab title="🗺️ 圖表" %} <img src="https://2527454625-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfvEFZnSBhKT6fJmus0%2Fuploads%2FSrzahlasA7PWTpTjnyuU%2Fprefer.string.svg?alt=media&#x26;token=724a2745-72e2-4d65-80be-7c680028413e" alt="" class="gitbook-drawing">

<img src="https://2527454625-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfvEFZnSBhKT6fJmus0%2Fuploads%2F26YEKDyxMhocZ9gcxWiA%2Fprefer.number.svg?alt=media&#x26;token=b4876750-4d01-48cf-8732-c1ec0ec59372" alt="" class="gitbook-drawing">
{% endtab %}

{% tab title="🔴 主題" %}

* [prefer-string](https://lochiwei.gitbook.io/web/js/val/obj/convert/obj-to-prim/prefer-string "mention")
* [prefer-number](https://lochiwei.gitbook.io/web/js/val/obj/convert/obj-to-prim/prefer-number "mention")
* [no-preference](https://lochiwei.gitbook.io/web/js/val/obj/convert/obj-to-prim/no-preference "mention")
  {% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="success" %}

* <mark style="color:red;">**object**</mark> -> [<mark style="color:yellow;">**bool**</mark>](https://lochiwei.gitbook.io/web/js/val/prim/boolean)：:point\_right: [falsy](https://lochiwei.gitbook.io/web/js/val/type/falsy "mention") values
* <mark style="color:red;">**object**</mark> -> [<mark style="color:yellow;">**number**</mark>](https://lochiwei.gitbook.io/web/js/val/prim/num)：:point\_right: [Broken link](https://lochiwei.gitbook.io/web/js/val/obj/convert/broken-reference "mention")
* <mark style="color:red;">**object**</mark> -> [<mark style="color:yellow;">**string**</mark>](https://lochiwei.gitbook.io/web/js/val/prim/str)：:point\_right: [Broken link](https://lochiwei.gitbook.io/web/js/val/obj/convert/broken-reference "mention")
  * <mark style="color:yellow;">**pass**</mark> an <mark style="color:red;">**object**</mark> to a <mark style="color:blue;">**built-in function**</mark> that expects a <mark style="color:yellow;">**string**</mark>.
  * <mark style="color:yellow;">**call**</mark> [<mark style="color:blue;">**String**</mark>](https://lochiwei.gitbook.io/web/js/val/prim/str)()
  * <mark style="color:yellow;">**interpolate**</mark> <mark style="color:red;">**objects**</mark> into [template-literal](https://lochiwei.gitbook.io/web/js/val/prim/str/template-literal "mention").
    {% endhint %}

{% hint style="info" %}
[date](https://lochiwei.gitbook.io/web/js/val/builtin/date "mention") is the <mark style="color:red;">**only**</mark>**&#x20;**<mark style="color:yellow;">**built-in type**</mark> that uses "<mark style="color:yellow;">**prefer-**</mark><mark style="color:red;">**string**</mark>" <mark style="color:red;">**object**</mark>**&#x20;**<mark style="color:yellow;">**-> primitive**</mark>**&#x20;**<mark style="color:purple;">**conversion**</mark>, <mark style="color:red;">**all other**</mark>**&#x20;**<mark style="color:yellow;">**types**</mark> uses "<mark style="color:yellow;">**prefer-**</mark><mark style="color:green;">**number**</mark>".
{% endhint %}
{% endtab %}

{% tab title="👥 相關" %}

* :point\_right: [boolean](https://lochiwei.gitbook.io/web/js/val/prim/boolean "mention") <mark style="color:yellow;">**/**</mark> [num](https://lochiwei.gitbook.io/web/js/val/prim/num "mention") <mark style="color:yellow;">**/**</mark> [str](https://lochiwei.gitbook.io/web/js/val/prim/str "mention")
* :arrow\_down: <mark style="color:purple;">**object -> primitive algorithms**</mark> are <mark style="color:yellow;">**involved**</mark> in：
  * [+](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/binary/+ "mention").
  * [sloppy-equality](https://lochiwei.gitbook.io/web/js/grammar/op/relational/equal/sloppy-equality "mention")
  * [compare](https://lochiwei.gitbook.io/web/js/grammar/op/relational/compare "mention").
* :no\_entry: <mark style="color:purple;">**object -> primitive algorithms**</mark> may result in [<mark style="color:red;">**TypeError**</mark>](https://lochiwei.gitbook.io/web/js/err/type):exclamation:
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] [javascript-the-definitive-guide](https://lochiwei.gitbook.io/web/master/ref/javascript-the-definitive-guide "mention") ⟩&#x20;
  * [x] 3.9.2 Explicit Conversions
  * [x] 3.9.3 Object to Primitive Conversions
  * [ ] 14.4.7 (define your own object -> primitive conversion)
    {% endtab %}

{% tab title="📘 手冊" %}

* [Object.prototype.toString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString) - object to string.
* [Object.prototype.valueOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf) - object to (primitive) value, if exists.
  {% endtab %}
  {% endtabs %}


---

# 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://lochiwei.gitbook.io/web/js/val/obj/convert/obj-to-prim.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.
