About the "===" and "!==" operators:

https://www.codecademy.com/courses/learn-javascript-control-flow/lessons/control-flow/exercises/comparison-operators-ii?action=lesson_resume&course_redirect=introduction-to-javascript

Hello. Just wanted to clarify something important about comparison operators, specifically the “===” and “!==”.

I have a C++ background, and I’m sure many of you guys too. As far as I know C++ and some other languages do not support this operators and we rather use “==” and “!=”.
However, I wanted to dig into this topic because JS supports both types of operators.(this meaning ===, !==, == and !=.)

Quoting Douglas Crockford's excellent work : JavaScript: The Good Parts,

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. 
The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:

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

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

false == undefined  // false
false == null       // false
null == undefined   // true

' \t\r\n ' == 0     // true
The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator.

I also find this interesting. Your quote is cut off a bit. The author seems to indicate that the operators common to other languages (==,!=,etc) are the “evil twins”. He seems to suggest not to use them. In fact it’s in bold at the bottom :wink:

I would be interested in knowing why. Based on this snip of information (and the class on this site), it would seem to be “best practice” to use the triple operators in JavaScript.

I can accept that as truth. However, I am curious as to why it is true. My guess is support for the double operators is there for programmers of other languages and, perhaps, legacy programmers. But, I would love to hear a good explanation as to why the triple operators are superior in JavaScript.

Perhaps your book can provide some insight?

:smiley: X

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.