In JavaScript is != same as !==

JavascriptSyntax

Javascript Problem Overview


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

Look at this commit

Is != same as !== in JavaScript?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

From

http://en.wikipedia.org/wiki/JavaScript_syntax#Operators

!== Not identical

!= Not equal

AND "Identical means equal and of same type."

From

http://docstore.mik.ua/orelly/webprog/jscript/ch05_04.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.

Solution 3 - Javascript

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)

Solution 4 - Javascript

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.

Solution 5 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRogerView Question on Stackoverflow
Solution 1 - JavascriptAmadiereView Answer on Stackoverflow
Solution 2 - JavascriptmartinrView Answer on Stackoverflow
Solution 3 - JavascriptFortegaView Answer on Stackoverflow
Solution 4 - JavascriptSean DevlinView Answer on Stackoverflow
Solution 5 - JavascriptSarfrazView Answer on Stackoverflow