Posted by on 11th Oct 2017

title: JavaScript Cheat Sheet subheading: Oh JavaScript, how my head weeps for thee, the language saved by babel. Things to remember...

Equality Operators

It appears with JavaScript you have to be very careful using the simple equality operatory '==', according to
TBD: this reference

If either values of the evaluation don't match types (which is very easy in the non typed language). In JavaScript the following statement is true:

if ("False" == true) {
    console.log("This is a true statement");

Seems weird, right? The equality operator '==' really seems to be acting like an 'and' statement (I'm sure I'm missing something here, but ...).

We do know that using the "===", or what I call the really equals operator.

Hence, the following code works as I would think it should work:

if ("False" === true) {
    console.log("False statement, this code is never reached")
} else if ("True" === true) {
    console.log("Still false, unreachable");
} else if ("True" === "True") {
    console.log("True statement, yea!  This code is reachable!");

So, basically I know now that I will most likely use "===", even when I don't really need to just to be safe.

Oh, well, I'm no language lawyer, so I'll continue satisfied with this explanation. Besides, the Jetbrains IDE's actually tell me to change the '==' to '===', so I'm good. :)

You can also use the string.equals(otherstring) method to avoid all of this for strings.

That's it for now. Use '===' in JavaScript most of the time. If you do use the '==', know why it is safe.

By The Way this all applies in opposite for the not ('!')forms of equality.

For Smart People and CS Majors

Reading the article above says that '==' performs the check with coercion.

Your homework is to define what coercion is and how it effects the equality evaluations between the JavaScript '==' and '===' operators.

Bonus points: show a legit example when the '==' can be used where the '===' would not work.