> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/obj/convert/obj-to-prim.md).

# object -> primitive conversion

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

{% hint style="success" %}
[JS spec](/web/master/ref/js-spec.md) 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>](/web/js/val/obj/convert/obj-to-prim/prefer-string.md)：preferring a string (if conversion is possible)
* [<mark style="color:yellow;">**prefer-**</mark><mark style="color:green;">**number**</mark>](/web/js/val/obj/convert/obj-to-prim/prefer-number.md)：preferring a number (if conversion is possible)
* [**no-preference**](/web/js/val/obj/convert/obj-to-prim/no-preference.md)：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&amp;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&amp;token=b4876750-4d01-48cf-8732-c1ec0ec59372" alt="" class="gitbook-drawing">
{% endtab %}

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

* [prefer-string](/web/js/val/obj/convert/obj-to-prim/prefer-string.md)
* [prefer-number](/web/js/val/obj/convert/obj-to-prim/prefer-number.md)
* [no preference](/web/js/val/obj/convert/obj-to-prim/no-preference.md)
  {% endtab %}

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

* <mark style="color:red;">**object**</mark> -> [<mark style="color:yellow;">**bool**</mark>](/web/js/val/prim/boolean.md)：:point\_right: [falsy](/web/js/val/type/falsy.md) values
* <mark style="color:red;">**object**</mark> -> [<mark style="color:yellow;">**number**</mark>](/web/js/val/prim/num.md)：:point\_right: [Broken mention](broken://pages/l1v0bwCArjuB3OOG3NfE)
* <mark style="color:red;">**object**</mark> -> [<mark style="color:yellow;">**string**</mark>](/web/js/val/prim/str.md)：:point\_right: [Broken mention](broken://pages/l1v0bwCArjuB3OOG3NfE)
  * <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>](/web/js/val/prim/str.md)()
  * <mark style="color:yellow;">**interpolate**</mark> <mark style="color:red;">**objects**</mark> into [template literal](/web/js/val/prim/str/template-literal.md).
    {% endhint %}

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

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

* [ ] [JavaScript: The Definitive Guide](/web/master/ref/javascript-the-definitive-guide.md) ⟩&#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 %}
