1

Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
Javascript operator !==

What's the difference between != and !==?

Can you give me an example where using != gives another result than using !==?

Community
  • 1
  • 1
Mageek
  • 3,451
  • 4
  • 32
  • 54
  • 2
    http://stackoverflow.com/questions/6559358/what-is-the-difference-between-and?rq=1 should help – 4444 Aug 14 '12 at 20:57

1 Answers1

7
alert(1 != true);
alert(1 !== true);

The first one is false, the second true.

  • != accept 1 as equals of true, null as equals of false and some others (because the values are automatically casted when being compared).
  • !== accept only "real" equalities (i.e. compares both the value and the type).

Example

PeeHaa
  • 66,697
  • 53
  • 182
  • 254
Diego
  • 15,566
  • 24
  • 79
  • 132
  • 3
    That's not really an answer though. – Lee Taylor Aug 14 '12 at 20:58
  • I clicked by accident.. sorry. – Diego Aug 14 '12 at 21:00
  • @Diego Then, if I am comparing variables (of which I don't know the value), should I always use `!==`? – Mageek Aug 14 '12 at 21:01
  • It depends the case. If you have a boolean variable its actually the same. If you have an integer you should use `!==` to compare it with integers or `!=` to "auto cast it" to boolean – Diego Aug 14 '12 at 21:03
  • `null` never equals `false` in an equality comparison. –  Aug 14 '12 at 21:07
  • @amnotiam You are actually right `(null == false)` is `false`. I got confused because `(null)` (if evaluated as boolean) is `false` and `(!null)` `true`. – Diego Aug 15 '12 at 11:44