6

Given the type coercion and the performance issue, as a JS newbie, I've always tried to avoid using double equality and opted for triple equality instead.

However, when does it make sense to use double quality?

katus
  • 649
  • 1
  • 6
  • 16
  • You use strict equality (===) only when you want to guarantee that the values of the operands are equal and their data types are also of the same type. The double equality (==) only checks for whether the values are the same, particularly numerical values with implicit conversion, and disregard the data types. Triple equality should always work but not without going the extra step of converting one or both to the same data type first. – Christian Hur May 25 '18 at 22:17

3 Answers3

3

Short answer: it never makes sense to use == instead of ===.

Long answer: while it never makes sense, there are cases where you want to write less code, and you are sure about your input.

== is not that bad if you really understand what's truthy. There are some gotchas, for example [1] == true and [2] == false.

Remember that the principal aim of Javascript was to be an easy language to be used by web designers, that's one of the reasons behind all this stuff you encounter.

As a simple example, as Barmar said, the best approach is to use it to avoid converting data and check equality. Here is another example:

const deleteTodos = _ => {
    if (howManyTasksToRemoveInput.value == tasks.length) {
        warningLabel.innerHTML = "You can't remove all the list";
    } else if (howManyTasksToRemoveInput.value > tasks.length) {
        warningLabel.innerHTML = "You DEFINITELY can't remove more tasks than you have";
    } else {
        tasks.splice(0, +howManyTasksToRemoveInput.value);
    }
}
Jorjon
  • 5,316
  • 1
  • 41
  • 58
  • But here again, the OP said _“I've always tried to avoid using double equality and opted for triple equality instead.”_ With this in mind, the OP would rather use `Number(howManyTasksToRemoveInput.value) === tasks.length` or `howManyTasksToRemoveInput.valueAsNumber === tasks.length`. The quantity “how many tasks to remove” really should be a number anyway. – Sebastian Simon May 27 '18 at 05:03
  • What's your point? Of course `==` can be replaced by `===`, that's why a lot people using linters enforce to use `===` only. There's not a single place in Javascript where `==` is accepted that you can't use `===`. My point was that this is the case since Javascript was made to be easy. I don't see your point. – Jorjon May 28 '18 at 15:35
  • The OP is opting to avoid `==` in favor of `===` and asks about when it ever makes sense to use `==` (if you can always use a stricter equality, or use more consistent data types, etc.). _“`==` can be replaced by `===`”_ and _“there’s not a single place […]”_ are great points against using `==`. That was exactly my point. The fact that JavaScript was designed to be an easy language may be a good reason for the existence of `==`, indeed. – Sebastian Simon May 28 '18 at 15:44
  • @Xufox gotcha, added a summary to be precise on the answer – Jorjon May 29 '18 at 20:56
3

There's one case where there's a clear benefit to using ==, which is foo == null: this is true if and only if foo === undefined || foo === null, a commonly useful test.

Even here, using something like node's isNullOrUndefined(foo) (which is implemented as return value == null;!) is a much clearer way to express the intent, if it's not an established practice in the code-base you're working in.

Simon Buchan
  • 12,707
  • 2
  • 48
  • 55
1

When you want to allow Javascript to convert types automatically. E.g.

if (someElement.value == 3)

value of an input is always a string. The == operator will automatically convert it to a number here, so you don't have to write

if (parseInt(someElement.value) === 3)

You should be careful, since some some of the automatic conversions may not do what you expect.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • But then, why not use `=== "3"`? Or if `3` is going to be a fixed number, why not indeed just parse the input’s value into a number, if you know what types you need to compare anyway, and you don’t want any unexpected conversions? – Sebastian Simon May 25 '18 at 22:23
  • 1
    There are lots of ways to skin the cat. – Barmar May 25 '18 at 22:24