Back to Biomejs

noDoubleEquals

src/content/docs/linter/rules/no-double-equals.mdx

latest4.0 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/suspicious/noDoubleEquals`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`eqeqeq`](https://eslint.org/docs/latest/rules/eqeqeq)

How to configure

json
{
	"linter": {
		"rules": {
			"suspicious": {
				"noDoubleEquals": "error"
			}
		}
	}
}

Description

Require the use of === and !==.

It is generally bad practice to use == for comparison instead of ===. Double operators will trigger implicit type coercion and are thus not preferred. Using strict equality operators is almost always best practice.

For ergonomic reasons, this rule makes by default an exception for == null for comparing to both null and undefined.

Examples

Invalid

js
foo == bar
<pre class="language-text"><code class="language-text">code-block.js:1:5 <a href="https://biomejs.dev/linter/rules/no-double-equals">lint/suspicious/noDoubleEquals</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Using </span><span style="color: Tomato;"><strong>==</strong></span><span style="color: Tomato;"> may be unsafe if you are relying on type coercion.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>foo == bar <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;"><strong>==</strong></span><span style="color: lightgreen;"> is only allowed when comparing against </span><span style="color: lightgreen;"><strong>null</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Use </span><span style="color: lightgreen;"><strong>===</strong></span><span style="color: lightgreen;"> instead.</span> <strong> 1 │ </strong>foo<span style="opacity: 0.8;">·</span>==<span style="color: MediumSeaGreen;">=</span><span style="opacity: 0.8;">·</span>bar <strong> │ </strong> <span style="color: MediumSeaGreen;">+</span> </code></pre>

Valid

js
foo == null
js
foo != null
js
null == foo
js
null != foo

Options

The rule provides the option described below.

json
{
    "//":"...",
    "options": {
        "ignoreNull": true
    }
}

ignoreNull

When this option is set to true, an exception will be made for checking against null, as relying on the double equals operator to compare with null is frequently used to check equality with either null or undefined.

When the option is set to false, all double equal operators will be forbidden without exceptions.

Default: true

</TabItem> </Tabs>