1

=== compares two values and makes sure the type is the same while == does not care about the type. In the book "JavaScript the good parts" == is called "the evil twin" if I am not remembering wrong.

So can you give examples of use cases when you need to use == instead of ===?

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
pethel
  • 5,397
  • 12
  • 55
  • 86
  • 2
    For example, when you get a string input that you need to compare to an integer (and you're too lazy to use `parseInt`)... `"3" == 3` – Shomz Jan 30 '13 at 07:47
  • possible duplicate of [JavaScript === vs == : Does it matter which "equal" operator I use?](http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use) – Peter O. Jan 30 '13 at 07:48
  • I'm not sure that's a dupe, Peter. The question you linked just compares the two, but doesn't really give advice when it's actually ok to use `==`. – Joey Jan 30 '13 at 07:49
  • I really can't think of a scenario where you "need" to use `==`. In most cases, not being lazy is the better option. (So, make sure the types are right before comparing) – Cerbrus Jan 30 '13 at 07:58

6 Answers6

1

If you know the comparison is with the same type, then == is just the same as ===, but save you one char.

For example, you could just do typeof foo == 'function' instead of typeof foo === 'function', because typeof will always return a string.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • But if you know it is the same type then why not just use ===? – pethel Jan 30 '13 at 07:56
  • @user874774, if you know it is the same type why not just use `==`? ;) – Dagg Nabbit Jan 30 '13 at 07:56
  • @user874774 `typeof foo` always produces a string... it will never produce anything else. `typeof foo == "string literal"` is pretty much identical to `typeof foo === "string literal"` in every way, the difference is just a matter of style. Even so, this has been endlessly debated here and elsewhere, for some reason... – Dagg Nabbit Jan 30 '13 at 08:07
1

As you pointed out, == compares the value of two objects, whereas === compares the type as well.

The problem with weak typing is that you can't always be 100% sure of what type a variable may be. For example, you might want to extract a number from a string. As your code gets more and more complicated, it gets harder and harder to keep track of what variables are of what type in what places in your code.

So in comes ==. "1" == 1 returns true, but "1" === 1 does not. This is generally a good thing. While it is in the end a question of personal choice which operator to use, the generally accepted usage is to use == by default, and === when that simply won't suffice.

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
  • Loosing control does not seem to be a good thing. I would have used Number("1") === 1 instead i guess. – pethel Jan 30 '13 at 07:55
  • It depends. Personally I agree with the "losing control" which is why I try to stick to strongly typed languages whenever possible. That said, the question is **when do you need to compare types?** If you don't care about the type than `===` will cause more problems than it solves. – Levi Botelho Jan 30 '13 at 08:01
  • 1
    @user874774 this is a great example of a case where `===` can bite you in the behind if you're not careful. Add `new` to that and you're comparing an object with a primitive... another developer might come behind you and think "hmm, this should use `new`." – Dagg Nabbit Jan 30 '13 at 08:01
  • If you want a real-life example of when this could happen, think about parsing some kind of formatted data like XML. Because you don't have strong typing in JS you won't be notified if you forgot to cast that "1" to an int and you won't be able to figure out why your code isn't working for you. Like I said before, comparing the types here will often serve no purpose, because the type doesn't matter. If you are parsing a numeric value from a string response, "1" is in practice equal to 1. – Levi Botelho Jan 30 '13 at 08:07
0

When you want to check for both value and type use ===. If you want to check only the value use ==

For more info on Comparison Operators in javascript

Raja Asthana
  • 2,080
  • 2
  • 19
  • 35
0

Since you are specifically asking for examples, not explanations... My most common use-case for === is something like this:

function isFoo(val) {
    if(val) // checks val 'exists'
    {
        return (val == 'foo'); // returns true or false
    }
    //'returns' undefined
}

var checkResult = isFoo('bar');

if(checkResult  === true) {
    alert('The value is foo');
}
else if(checkResult === false) {
    alert('The value exists, but is not foo');
}
else { // result was undefined
    alert('No value was provided, or it was null/undefined/false');
}

It's a lazy way to go about it, but it works pretty nicely. In practice, generally you rarely need to use ==, you're much more likely to need to use ===.

Bob Davies
  • 2,229
  • 1
  • 19
  • 28
0

It is almost always better to use the === and !== operators.

The == and != operators do type coercion for eg. they will force a string to be evaluated as a number.

And in the case of ===, since the string "1" is not the same as the numeric value 1, the result is false.

See this also.

sohel khalifa
  • 5,602
  • 3
  • 34
  • 46
0

Your question, emphasis mine:

...can you give examples of use cases when you need to use == instead of ===?

This is sort of a loaded question. You can emulate == by combining === with other code, and as long as you can do that, you never really "need" ==. This is sort of like asking for cases where you "need" ++, maybe because you prefer to write it as foo += 1.

Your title asks a more practical question:

When is it a good idea to use == instead of === in js?

Any time you find yourself emulating the behavior of == with ===, you should probably just be using ==. This includes most cases where you are converting both sides of the operation to the same type, and then comparing with ===. That's what == is made for, why not use it?

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141