# object -> primitive conversion

[JS](../../..) ⟩ [value](../..) ⟩ [type](../../type) ⟩ [conversion](../../type/convert) ⟩ object -> primitive

{% hint style="success" %}
[js-spec](../../../../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>](obj-to-prim/prefer-string)：preferring a string (if conversion is possible)
* [<mark style="color:yellow;">**prefer-**</mark><mark style="color:green;">**number**</mark>](obj-to-prim/prefer-number)：preferring a number (if conversion is possible)
* [**no-preference**](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](obj-to-prim/prefer-string "mention")
* [prefer-number](obj-to-prim/prefer-number "mention")
* [no-preference](obj-to-prim/no-preference "mention")
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="success" %}
* <mark style="color:red;">**object**</mark> -> [<mark style="color:yellow;">**bool**</mark>](../../prim/boolean)：:point\_right: [falsy](../../type/falsy "mention") values
* <mark style="color:red;">**object**</mark> -> [<mark style="color:yellow;">**number**</mark>](../../prim/num)：:point\_right: [Broken link](broken-reference "mention")
* <mark style="color:red;">**object**</mark> -> [<mark style="color:yellow;">**string**</mark>](../../prim/str)：:point\_right: [Broken link](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>](../../prim/str)()
  * <mark style="color:yellow;">**interpolate**</mark> <mark style="color:red;">**objects**</mark> into [template-literal](../../prim/str/template-literal "mention").
{% endhint %}

{% hint style="info" %}
[date](../../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](../../prim/boolean "mention") <mark style="color:yellow;">**/**</mark> [num](../../prim/num "mention") <mark style="color:yellow;">**/**</mark> [str](../../prim/str "mention")
* :arrow\_down: <mark style="color:purple;">**object -> primitive algorithms**</mark> are <mark style="color:yellow;">**involved**</mark> in：
  * [+](../../../grammar/op/arithmetic/binary/+ "mention").
  * [sloppy-equality](../../../grammar/op/relational/equal/sloppy-equality "mention")
  * [compare](../../../grammar/op/relational/compare "mention").
* :no\_entry: <mark style="color:purple;">**object -> primitive algorithms**</mark> may result in [<mark style="color:red;">**TypeError**</mark>](../../../err/type):exclamation:
{% endtab %}

{% tab title="📗 參考" %}
* [ ] [javascript-the-definitive-guide](../../../../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 %}
