78

Possible Duplicates:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
What is the difference between != and !== operators in JavaScript?

Look at this commit:

Is != the same as !== in JavaScript?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Roger
  • 3,919
  • 7
  • 30
  • 37
  • 9
    See http://stackoverflow.com/questions/359494/javascript-vs – Pool Dec 22 '09 at 12:31
  • 8
    @S.Lott: He needed to have known that they were called "Javascript comparison operators" for one thing - which the question doesn't imply he does (to be fair to the original poster). – Amadiere Dec 22 '09 at 12:39
  • 4
    Try to type in `!==` in google and see what results you get. Even `javascript !==` - so google won't help – Mottie Dec 22 '09 at 12:42
  • They are different: one has an extra = – Adriano Varoli Piazza Dec 22 '09 at 12:51
  • 2
    Another dupe specific to `!=`: http://stackoverflow.com/questions/1889260/javascript-operator – Crescent Fresh Dec 22 '09 at 13:40
  • Who's cranky? I was meerly trying to support the OP... you can't search google or SO for operators. – Mottie Dec 22 '09 at 14:06
  • 1
    @fudgey: You can search Google for a lot of things.. maybe he didnt find it.. SLott's comment is off base –  Dec 22 '09 at 14:26
  • You aren't supposed to up-vote duplicate questions. Close them and point them to the previously asked question so we don't clutter the database. – Josh Stodola Dec 22 '09 at 15:08

5 Answers5

130

They are subtly not the same.

!= checks the value
!== checks the value and type

'1' != 1   // false (these two are the same)
'1' !== 1 // true (these two are **not** the same).

In the previous example. The first half of the expression is a string, the second half is an integer.

Amadiere
  • 11,313
  • 6
  • 41
  • 47
15

From JavaScript syntax, Operators,

!== Not identical

!= Not equal

AND "Identical means equal and of same type."

From 5.4. Equality Operators:

"In JavaScript, numbers, strings, and boolean values are compared by value. ... On the other hand, objects, arrays, and functions are compared by reference. "

--

So in summary, are they the same? No, because there is an additional test with !== (over !=) for type sameness as well as equalness.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
martinr
  • 3,794
  • 2
  • 17
  • 15
10

No, it is not the same. See for example here.

4 !== '4' returns true   (and 4 === '4' returns false)
4 != '4'  returns false  (and 4 == '4'  returns true)
Fortega
  • 19,463
  • 14
  • 75
  • 113
  • 2
    And for the full skinny, there's nothing like the spec. You can download the latest from here: http://www.ecma-international.org/publications/files/drafts/ It's the PDF file starting with "tc39-" (as of this writing, tc39-2009-050.pdf). That says it's a draft, but it was voted through earlier this month. – T.J. Crowder Dec 22 '09 at 12:42
  • 2
    And see http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use/1813267#1813267 how to read the spec! – nalply Dec 22 '09 at 13:21
  • Hehe, the fact that you need a manual to read a spec really says something about the quality of the spec itself :) – Fortega Dec 22 '09 at 13:29
8

The big difference is that != performs type coercion. That is, one value is effectively cast to the other before equality is checked. This is why, as in Amadiere's answer:

'1' != 1

evaluates to false. The same holds true for == v. ===. In general, avoid == and != unless you specifically want coercion to be performed. Use === and !== and check for exactly the result you're looking for.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean Devlin
  • 1,662
  • 12
  • 17
-1

It checks not only value, but also the type of the things compared.

This is also the same in PHP and some other languages too.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sarfraz
  • 377,238
  • 77
  • 533
  • 578