This post covers examples and notes about the comparison of double equal signs(==) and triple equal signs(===) operators in JavaScript.
First, what do equal signs about?
Both operators are used to compare different values.
== is called equality operator, which applies type coercion, what doe type coercion, It converts the values before comparison
m==n returns true if m and n are the same values and types, else false.
=== is called identity operators, which do not apply type coercion, not converting the values before conversion.
m===n returns true if m and n are the same values for strings and the same reference for objects, else false.
== and === operator Examples
Here are some examples of double equal and three equal operator examples
5=="5" - true
5==="5" - false
'0' == undefined // true
'0' === undefined // false
null == undefined // true
null === undefined // false
difference between == vs === operators
Both operators are applied two operands or variables
== operator | === operators |
---|---|
Called Comparison operator | Strict comparison operator |
compared two operands or variables, the data type is ignored | compared two operands or variables and data type |
|Returns true, if values are equal, false not equal| Returns true, if values and types are equal, false both not equal| |duration| time in milliseconds shown before disappearing from the page| |type coercion before comparison| type coercion ignored before comparison|