# sloppy equality (==)

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [statement](https://lochiwei.gitbook.io/web/js/grammar/statement) ⟩ [expression](https://lochiwei.gitbook.io/web/js/grammar/statement/expr) ⟩ [operator](https://lochiwei.gitbook.io/web/js/grammar/op) ⟩ [relational](https://lochiwei.gitbook.io/web/js/grammar/op/relational) ⟩ sloppy (==)

{% hint style="success" %}
perform <mark style="color:purple;">**equality**</mark> testing that <mark style="color:yellow;">**allows**</mark> [convert](https://lochiwei.gitbook.io/web/js/val/type/convert "mention").

:star2:[Broken link](https://lochiwei.gitbook.io/web/js/grammar/op/relational/equal/broken-reference "mention")
{% endhint %}

{% tabs %}
{% tab title="🗺️ 圖表" %}

* :point\_right: <mark style="color:yellow;">**compare**</mark>：[strict-equality](https://lochiwei.gitbook.io/web/js/grammar/op/relational/equal/strict-equality "mention")

<img src="https://2527454625-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfvEFZnSBhKT6fJmus0%2Fuploads%2F6P1j9pporlhQkTNF9xbl%2Fsloppy.equality.svg?alt=media&#x26;token=88cadb12-cdf1-4c83-810a-73b4e1004098" alt="" class="gitbook-drawing">
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="warning" %}
[<mark style="color:blue;">**convertibility**</mark>](https://lochiwei.gitbook.io/web/js/val/type/convert) <mark style="color:yellow;">**does**</mark>**&#x20;**<mark style="color:red;">**not**</mark>**&#x20;**<mark style="color:yellow;">**imply**</mark>**&#x20;**<mark style="color:purple;">**equality**</mark>:exclamation:

```javascript
// ⭐️ `undefined` can be converted to `false`.
undefined == false     // false❗ 
```

{% endhint %}

{% hint style="warning" %} <mark style="color:purple;">**`==`**</mark> <mark style="color:red;">**never**</mark> [<mark style="color:yellow;">**converts**</mark>](https://lochiwei.gitbook.io/web/js/val/type/convert) its operands to [<mark style="color:yellow;">**booleans**</mark>](https://lochiwei.gitbook.io/web/js/val/prim/boolean):exclamation:
{% endhint %}

{% hint style="warning" %}
[<mark style="color:purple;">**`<=`**</mark> and <mark style="color:purple;">**`>=`**</mark>](https://lochiwei.gitbook.io/web/js/grammar/op/relational/compare) <mark style="color:red;">**do not**</mark>**&#x20;**<mark style="color:yellow;">**rely on**</mark> <mark style="color:blue;">**`==`**</mark> or <mark style="color:blue;">**`===`**</mark>:exclamation:
{% endhint %}
{% endtab %}

{% tab title="💈範例" %}

* replit： [sloppy equality (==)](https://replit.com/@pegasusroe/sloppy-equality#index.js)

```javascript
// ⭐️ sloppy equality (==)
null == undefined,  // true
0    == '0',        // true    ('0'   -> 0)
0    == false,      // true    (false -> 0)
1    == true,       // true    (true  -> 1)
'1'  == true,       // true    (true  -> 1, then '1' -> 1)
'0'  == false,      // true    (false -> 0, then '0' -> 0)

// ⭐️ convertibility doesn't imply equality
//   (undefined can be converted to false)
undefined == false, // false❗ 
```

{% endtab %}

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

* :star: [convert](https://lochiwei.gitbook.io/web/js/val/type/convert "mention")
* :star2: [Broken link](https://lochiwei.gitbook.io/web/js/grammar/op/relational/equal/broken-reference "mention")
* [n.isequal](https://lochiwei.gitbook.io/web/js/val/prim/num/number+ext/n.isequal "mention")
* :information\_source: the following are <mark style="color:yellow;">**involved**</mark> in <mark style="color:purple;">**`==`**</mark> operation.
  * [strict-equality](https://lochiwei.gitbook.io/web/js/grammar/op/relational/equal/strict-equality "mention")&#x20;
  * [obj-to-prim](https://lochiwei.gitbook.io/web/js/val/obj/convert/obj-to-prim "mention")&#x20;
* :scales: [assignment](https://lochiwei.gitbook.io/web/js/grammar/op/assign/assignment "mention")
  {% endtab %}

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

* [ ] [javascript-the-definitive-guide](https://lochiwei.gitbook.io/web/master/ref/javascript-the-definitive-guide "mention") ⟩&#x20;
  * [x] 3.9.1 Conversions & Equality
  * [ ] 4.9.1 ⭐️
    {% endtab %}
    {% endtabs %}
