Development · 7 min read

Strict vs loose equality in JavaScript

JavaScript has two equality operators that look almost identical but behave very differently: === (strict) and == (loose). Strict compares value and type. Loose compares value after coercing the types to match. The result is that 1 === "1" is false and 1 == "1" is true. That one-character difference can change how your code behaves, so it’s worth knowing exactly when to use each.

Strict equality (===)

Strict equality, also known as “triple equals”, compares both the value and the type of two operands. It returns true if the operands are equal and of the same type.

JavaScript strict equality comparison table

Source: JavaScript equality table by Dorey.

Strict equality example:

1 === 1 // true
1 === "1" // false

In the first example, both operands are of the same type (number) and have the same value, so the comparison returns true. In the second example, one operand is a number and the other is a string, so the comparison returns false.

Loose equality (==)

Loose equality, also known as “double equals”, compares the values of two operands without considering their types. It returns true if the operands are equal, regardless of their type.

JavaScript loose equality comparison table

Source: JavaScript equality table by Dorey.

Loose equality example:

1 == 1 // true
1 == "1" // true

In the first example, both operands are of the same type (number) and have the same value, so the comparison returns true. In the second example, one operand is a number and the other is a string, but their values are the same, so the comparison returns true.

When to use each one

In general, it’s best to use strict equality whenever possible because it avoids the confusion that type coercion can cause. There are a few cases where loose equality is useful:

// Use strict equality when comparing variables of the same type
let age = 25;
let legalAge = 21;
console.log(age === legalAge); // false

// Use loose equality when comparing variables that may be a string or a number
let input = "25";
let number = 25;
console.log(input == number); // true

// Use loose equality when comparing a variable to null or undefined
let name;
console.log(name == undefined); // true

A simple rule: default to ===. It’s predictable, it avoids the type coercion traps in the MDN sameness chart, and it makes your intent clear. Reach for == only when you genuinely need to treat string and number versions of the same value as equal, or when you want null and undefined to match in a single check. In JavaScript projects with a linter, eslint’s eqeqeq rule will flag every == for you, which is a good safety net.

More resources:

Share:

Your Friday WooCommerce briefing

What changed this week, what broke, and what you should try. Plugin news, store fixes, and opinions. No fluff, no affiliate spam.

Sent every Friday. Unsubscribe in one click.

This blog is independent and ad-free. If a post saved you time or taught you something new, a coffee goes a long way.

Have thoughts, questions, or a different take? I'd love to hear from you.

Powered by Giscus · Sign in with GitHub to comment. · Privacy policy