I am Software Developer, currently interested in static type languages (TypeScript, Elm, ReScript) mostly in the frontend land, but working actively in Python also. I am available for mentoring.
Both are equality operators. One exists because of the failure of the second. I would say == is equality with type coercion === is equality without type coercion.
JavaScript is performing the equality evaluation. The interpreter first converts both operands to the same type. Then executes the identity comparison.
The identity evaluation algorithm ( ===) some rules
If both operands have different types, they are not strictly equal
If both operands are null, they are strictly equal
If both operands are undefined, they are strictly equal
If one or both operands are NaN, they are not strictly equal
If both operands are true or both false, they are strictly equal
If both operands are numbers and have the same value, they are strictly equal
If both operands are strings and have the same value, they are strictly equal
If both operands have reference to the same object or function, they are strictly equal
In all other cases operands are not strictly equal.
NaN in identity (and in equality) operator compared with any other value always evaluates to false. Let's consider some examples. Itβs the best way to remember the strict comparison algorithm.
1==="1"// false,
Operands are different types (number and string) and based on rule 1 they are not identical.
0===0// true, rule 6
Operands are the same type (number) and have the same value, so they are strictly equal based according to the rule in line 6.
undefined===undefined// true, rule 3
Both operands are undefined and applying rule 3 itβs an equality.
undefined===null// false, rule 1
Because operands are different types, based on rule 1 theyβre not identical.
NaN===NaN// false, rule 4 on line 4 -// identity evaluation algorithm
Operands are the same type (numbers), but the rule 4 indicates than nothing is equal with a NaN. The result isfalse.
varfirstObject={},secondObject=firstObject;secondObject['name']='Tom';secondObject===firstObject// true, based on rule 8
Both variables firstObject and secondObject are references to the same object and based on rule 8 the identity operator evaluates to true.
[]===[]//false,
The [ ] literal creates a new array reference.
Both operands being the same type (object), however have reference to different objects. rule 9 says that the identity evaluates to false.
Just pointing out that === is not the identity operator.
The === uses the Strict Equality Comparison algorithm, as opposed to == which uses the Abstract Equality Comparison algorithm.
You can easily tell that === is not an identity operator by considering a case like this:
I'm not sure where the idea that === is an identity operator comes from, but I've seen this mistake repeated many times. :)
Both are equality operators. One exists because of the failure of the second. I would say == is equality with type coercion === is equality without type coercion.
JavaScript is performing the equality evaluation. The interpreter first converts both operands to the same type. Then executes the identity comparison.
The identity evaluation algorithm ( ===) some rules
NaN
in identity (and in equality) operator compared with any other value always evaluates to false. Let's consider some examples. Itβs the best way to remember the strict comparison algorithm.Operands are different types (number and string) and based on rule 1 they are not identical.
Operands are the same type (number) and have the same value, so they are strictly equal based according to the rule in line 6.
Both operands are undefined and applying rule 3 itβs an equality.
Because operands are different types, based on rule 1 theyβre not identical.
Operands are the same type (numbers), but the rule 4 indicates than nothing is equal with a NaN. The result is
false
.Both variables firstObject and secondObject are references to the same object and based on rule 8 the identity operator evaluates to true.
The
[ ]
literal creates a new array reference.Both operands being the same type (object), however have reference to different objects. rule 9 says that the identity evaluates to
false
.Again, if NaN === NaN is false, === cannot be an identity operator. :)
You'll note that your rules have no "reference to" in them.
[] and [] just have different values - the value of an object is not determined by its properties.