In JavaScript, it's considered "best practice" to prefer the strict equality operator (===) over the equality operator (==). I still think this is good advice, but I've grown to prefer the equality operator in certain situations.

The difference

The equality operator attempts to convert values of different types before comparing them. The strict equality operator doesn't. For example, let's compare the number 52 with the string "52".

Using the equality operator, the comparison evaluates to true. This is because the number 52 can be converted to the string "52" and vice versa.

Using the strict equality operator, the comparison evaluates to false because numbers and strings are different types.

let num = 52;
let str = "52";

num == str; // true
num === str; // false

When to use the equality operator

I find the equality operator particularly useful when comparing numbers with strings.

For example, imagine we have an array of MESSAGES. Note that the value of each message's .id property is a number:

// The value of each message's `.id` property is a number.
const MESSAGES = [
{ id: 1, message: "Hello, World!" },
{ id: 2, message: "How are you, World?" },
{ id: 3, message: "Goodbye, World!" },
];

Imagine we're building a simple API using Express. When someone sends a request to the root of our API, i.e. /, we want to send the MESSAGES array in JSON format:

app.get("/", (_req, res) => {
res.json(MESSAGES);
});

When someone appends a number to the URL, e.g. /1, we want to send the message whose .id property matches that number. We can do this using route parameters and the Array.prototype.find() method. According to "best practice", we should use the strict equality operator. Because route parameters are strings, we have to explicitly convert the string to a number:

app.get("/:id", (req, res) => {
// The value of the `id` constant is a string.
const { id } = req.params;

// Using the strict equality operator, we have to convert the string to a number.
const message = MESSAGES.find((message) => message.id === Number(id));

if (!message) {
res.status(404).json({});
return;
}

res.json(message);
});

Why bother with explicit conversion? The equality operator takes care of it implicitly:

app.get("/:id", (req, res) => {
// The value of the `id` constant is a string.
const { id } = req.params;

// Using the equality operator, we DON'T have to convert the string to a number.
const message = MESSAGES.find((message) => message.id == id);

if (!message) {
res.status(404).json({});
return;
}

res.json(message);
});

View demo.

I agree that defaulting to the strict equality operator helps prevent bugs. But in situations like this, I don't think it adds any value. It just makes the code more verbose. I've already explained why I think type coercion is a good thing, so I rest my case.