Different between == and === in JavaScript examples
- Admin
- Dec 31, 2023
- Javascript
In this tutorial, We will have a look, at the difference between == and === in javascript with code examples.
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 an 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.
Javascript Equal operators(== and ===) 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
In the above examples,
5==“5”, Comparing number and string, and converted to the same type, i.e numbers, with == checks value(5==5) and type(numbers) are same, returns true. 5===“5” return false as both operands are different(integer and string)
javascript Comparision between == vs === operators
Both operators are applied to 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 |
