69

Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Difference between == and === in JavaScript

I have two variables to compare.

Result should not be equal, in which condition i need to use != and !== ?

because when i use both operator it is working properly, but i need to know exactly what is the difference.

Community
  • 1
  • 1
manny
  • 1,878
  • 2
  • 15
  • 31
  • yes, i couldn't find those old post, thx. – manny Dec 23 '11 at 13:14
  • If you want to have type coercion enabled (and you never want to), when use `!=`. Otherwise, use `!==`. – Šime Vidas Dec 23 '11 at 13:21
  • 2
    Honestly, it's not really a duplicate, because this is specific to != and !==, not == and ===. So if someone specifically wanted to know the difference to != and !==, then this would help. How would they know it has the same behavior as == and ===? – CrazyVideoGamer Feb 16 '21 at 23:47

3 Answers3

63

Human readable text about their differences

Using !== and === will do a more strict compare than ==/!=. The former will check if the objects being compared are of the same type, as well as if the values matches.

Using == will make it possible for an implicit cast to be made, see the below examples.

(0 ==  '0') // true
(0 === '0') // false

('' ==  0 ) // true, the string will implicitly be converted to an integer
('' === 0 ) // false, no implicit cast is being made

What does the standard say?

11.9.6 The Strict Equality Comparison

Algorithm The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is Number, then

    a. If x is NaN, return false.

    b.If y is NaN, return false.

    c. If x is the same Number value as y, return true.

    d. If x is +0 and y is 0, return true.

    e. If x is 0 and y is +0, return true.

    f. Return false.

  5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.

  6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  7. Return true if x and y refer to the same object. Otherwise, return false. NOTE This algorithm differs from the SameValue Algorithm (9.12) in its treatment of signed zeroes and NaNs.

11.9.3 The Abstract Equality Comparison Algorithm

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is the same as Type(y), then

    a. If Type(x) is Undefined, return t rue.

    b. If Type(x) is Null, return true.

    c. If Type(x) is Number, then

    1. If x is NaN, return false.
    
    2. If y is NaN, return false.
    
    3. If x is the same Number value as y, return true.
    
    4. If x is +0 and y is 0, return true.
    
    5. If x is 0 and y is +0, return true.
    
    6. Return false.
    

    d. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.

    e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false. f. Return true if x and y refer to the same object. Otherwise, return false.

  2. If x is null and y is undefined, return true.
  3. If x is undefined and y is null, return true.
  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
  8. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y) .
  9. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
  10. Return false
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
8

The difference is that the former (!=) version will coerce the two variables to be type compatible before the comparison. Hence:

"" == 0    -> true
"" === 0   -> false

The other version requires strict equality - the two values must both be of the same type and have the same value. Most of the time this is the one you should actually use.

In the case of objects strict equality means that they are actually the same object. A comparison between objects does not perform a field-by-field comparison of the contents of the object.

See https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators for more.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
6

The difference is that !== returns true only when variables have the same type and are not equal.

mysteRious
  • 4,102
  • 2
  • 16
  • 36
bjornd
  • 22,397
  • 4
  • 57
  • 73
  • This is incorrect. `1 !== "1"` also returns true. You mean: the difference is that !== returns true if the variables are equal but of different type. – murf Dec 20 '19 at 10:16
  • 1
    Compare the following two statements: `1 !== "1"` (true) and `1 != "1"` (false). – murf Dec 20 '19 at 10:24
  • you can't say 'true only', because 1 !== "1" and 1 !== "2" both are true. So proper statement is !== is true; when either value, type or both are different. Its become false only when you compare value with itself using !==. ie: 1 !== 1 is false, because both type and value are same. – Vaisakh Rajagopal Aug 28 '20 at 19:04