I will explain the difference between “==” and “===”.
“==” and “===” look the same, but there are differences in how they are used.
In this article, we will explain in detail the difference between “==” and “===” and explain when you should use one or the other.
Explanation of “==” and “===”
==
is an operator that tests whether values are equal.===
is an operator that strictly tests whether a value and type are equal.
For example, ==
if you compare “1” and “1” using , you true
will get , but ===
if you make the same comparison using , false
you will get . This is because “1” is a number and “1” is a string.
Another example is the result of “0 == false” “0 === false”. ==
It true
‘s going to be, but ===
it’s false
going to be. This is false
because “0” is a number, and “0” is a boolean value.
Check with sample program
The following sample program confirms the === and ==== behavior.
let num1 = 1;
let num2 = "1";
console.log(num1 == num2); // true
console.log(num1 === num2); // false
let num3 = 0;
let boolean1 = false;
console.log(num3 == boolean1); // true
console.log(num3 === boolean1); // false
let str1 = "hello";
let str2 = "world";
console.log(str1 == str2); // false
console.log(str1 === str2); // false
In the above sample, num1 and num2, num3 and boolean1, str1 and str2 are ==
compared ===
, and the results are displayed in console.log.
This way ==
you can determine whether values are equal, and ===
you can see that you can strictly determine whether values and types are equal.
!=!==
There are also corresponding operators .
Summary of “==” and “===”
==
I will summarize about this ===
.
==
is a value comparison===
is a value-type comparison==
converts the type and then compares===
compares without converting types
If you want to compare values and types strictly, use ===. On the other hand, === can be used when only values are to be compared.
By using them differently, you can make accurate judgments. Finally, remember that it is always important to be aware of the difference between === and ==== in programming and to make accurate comparisons.
I would prefer to use “====” because I want to compare values and types strictly.
Comments