How is the “==” and “===” operator in JavaScript different? In what cases to use them?

Tram Ho

The “==” in comparison operator is very familiar to programmers. In Javascript appear more “===”. So how are they similar and different? In this article, I will share with you all!

“==” operator

The “==” operator is used to check if the value of two elements is equal, but it only compares the value, not the data type Example:

10==10 // true
10==”10″ // true
“javascript”==”javasctipt” // true

In some other special cases when using “==”: + 0 has a numeric data type, but when compared with a boolean, it will be forced to a boolean with a value of false:

0 == false //true
0 == true //false

+ 1 has a numeric data type, but when compared with a boolean, it will be forced to a boolean with the value true:

1 == true //true
1 == false //false

+ “” has a data type of string, but when compared with a boolean, it will be forced to a boolean with the value false:

‘”” == true //false
‘”” == false //true

Operator “===”

The operator “===” also has the same thing as “==” that is also comparing in terms of values, but the difference is that “===” compares also on data types Example:

10===10 // true

But

10===”10″ // false

because 10 has a data type of number, and “10” has a data type of string

Conclude

So through the above article, I have shared about the similarities and differences between “==” and “===”. In case the operator is only interested in the value side, we use “==”, and when there are stricter conditions on the data type, we use “===”. In each practical problem we will encounter many cases, use “==” or “===” in the most effective way to match the requirements. Thank you for reading this article of mine, if you find it interesting, please help me 1 upvote.

Share the news now

Source : Viblo