1

I mean, in which situations and given which constraints do you really prefer (lax) equality defined by the == operator, rather than strict idendity defined by the === operator?

Sort of personal preference I know, but certainly not entirely!

So really, let's geniunely talk about the reasoned rationales and objective justifications for using either.

user777
  • 906
  • 5
  • 16
  • 1
    `if(obj.something==null)...` hits `null` and `undefined` in one shot, good for DOM properties. – dandavis Mar 10 '16 at 09:58
  • 1
    From performance point-of-view, using `===` (when possible) is better because the runtime wouldn't have to convert the value. – haim770 Mar 10 '16 at 09:59
  • Also, checking for strict `boolean` values: `1 == true` vs. `1 === true` – haim770 Mar 10 '16 at 10:01
  • tip: don't use `==true`, it's very complicated what results. `==false` is ok though... – dandavis Mar 10 '16 at 10:01
  • @dandavis I agree with you, however you might as well just go like this `if(!obj.something)` instead of `if(obj.something==null)` – AndreFeijo Mar 10 '16 at 10:04
  • 1
    rare, but possible: `new Number(3) === 3` is false, even though you might want/need it to be true. – dandavis Mar 10 '16 at 10:04
  • 1
    @AndreFeijo: no: `if(!obj.something)` would miss `0`, `false`, and `""`, all legit DOM properties. `==null` is how we can tell something is "missing" instead of falsey. – dandavis Mar 10 '16 at 10:05
  • @dandavis In some cases it's not a big deal. – AndreFeijo Mar 10 '16 at 10:10
  • 1
    [This](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons?rq=1) will help you! – The Reason Mar 10 '16 at 10:16
  • @AndreFeijo: one way to think of it is in cases where `obj.something` is _required_, `if(obj.something)` works fine, but if `.something` is optional in value but not in presence, `if(obj.something!=null)` makes sure it's there... – dandavis Mar 10 '16 at 10:16
  • @The haha wow, megathread, nice find thanks – user777 Mar 10 '16 at 10:18

0 Answers0