1

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

I've met "!==" strange comparison operator in source code of some chrome extension. Code snippet:

function closedTab(id) {
   if (openedTabs[id] !== undefined) {
      openedTabs[id].time = timeNow(0);
      closedTabs.unshift(openedTabs[id]);
   }
}

This operator is not used just once, so there's should some meaning.

Is "!==" came from some JavaScript magic? Or it's just equivalent to usual "!="? Thanks

Community
  • 1
  • 1
megas
  • 21,401
  • 12
  • 79
  • 130

5 Answers5

4

The difference is that !== doesn't try to convert its operands to the same type before comparing. Same with === and ==.

See this question: Difference between == and === in JavaScript

Community
  • 1
  • 1
Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
3

!== is the not identity comparison operator.

!= will coerce the two types to match

!== will not coerce the two types

For a few examples:

3 == "3"    // true - the operands are coerced to the same type, then compared and they match
3 === "3"   // false - the operands are not coerced to the same type, so do not match
3 != "3"    // false
3 !== "3"   // true
jfriend00
  • 683,504
  • 96
  • 985
  • 979
2

This is called a strict comparison operator where it not only checks for value but also for type.

65Fbef05
  • 4,492
  • 1
  • 22
  • 24
2

The difference comes from what happens when values are of different type.

The !== operator (and its cousin ===) checks on the equality of both value and type. != and == on the other hand try to coerce values to be the same before checking equality.

For example:

if(5 === "5")      // evaluates to false
if(5 == "5")       // evaluates to true

The same concept is extended to != and !==

riwalk
  • 14,033
  • 6
  • 51
  • 68
2

It's an identical comparison operator. It compares value and type. For example:

if('foobar' == true) // true
if('foobar' === true) // false
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145