Difference between == and === in JavaScript Difference between == and === in JavaScript - Solution Checker - solutionschecker.com - Find the solution for any programming question. We as a solution checker will focus on finding the fastest possible solution for developers. Main topics like coding, learning.
What is the difference between == and === in JavaScript? I have also seen != and !== operators. Are there more such operators?
Solution 1
Take a look here: http://longgoldenears.blogspot.com/2007/09/triple-equals-in-javascript.html
The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well.
0 == false // true
0 === false // false, because they are of a different type
1 == "1" // true, automatic type conversion for value only
1 === "1" // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false
Solution 2
=== and !== are strict comparison operators:
JavaScript has both strict and type-converting equality comparison. For
strictequality the objects being compared must have the same type and:
- Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
- Two numbers are strictly equal when they are numerically equal (have the same number value).
NaNis not equal to anything, includingNaN. Positive and negative zeros are equal to one another.- Two Boolean operands are strictly equal if both are true or both are false.
- Two objects are strictly equal if they refer to the same
Object.NullandUndefinedtypes are==(but not===). [I.e. (Null==Undefined) istruebut (Null===Undefined) isfalse]
