views:

306

answers:

6
+4  A: 

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
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
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
Hehe, the fact that you need a manual to read a spec really says something about the quality of the spec itself :)
Fortega
+31  A: 

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
Spiffing answer old chap
Andi
A: 

Checks not only value but also the type of the things compared. This is also same in php and some other languages too.

Sarfraz
+7  A: 

From

http://en.wikipedia.org/wiki/JavaScript%5Fsyntax#Operators

!== Not identical

!= Not equal

AND "Identical means equal and of same type."

From

http://docstore.mik.ua/orelly/webprog/jscript/ch05%5F04.htm

"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.

martinr
+3  A: 

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.

Sean Devlin
A: 

Have a look at JSLint for an explication of the difference. I would also advise you to pass your JavaScript code at least once through JSLint, you might learn valuable things...

Emilien