Skip to main content

Command Palette

Search for a command to run...

Day 4: Equality? In JavaScript?!

Updated
•5 min read•View as Markdown
Day 4: Equality? In JavaScript?!

So. Day 4. I walked in thinking I understood what "equal" means. I've been using the word "equal" my entire life. Turns out JavaScript and I have very different definitions.

I'm learning through the Nexus Web Development course by Rohit Negi sir on Coder Army, and today sir looked at us and said "== vs ===." Just like that. Like it was nothing. It was not nothing.

The loose friend and the strict bouncer

JavaScript has TWO ways to ask "are these equal?" And they do not agree.

10 == "10"    // true
10 === "10"   // false

Same values. Different answers. Why?

Because == — the loose friend — looks at the number 10 and the string "10" and goes "eh, close enough." It quietly converts the string into a number first, THEN compares. Bends the rules to make things work.

=== — the strict bouncer — checks your ID first. Number? String? Different species? "Not equal. Next." No conversion. No compromises. No mercy.

The internet's simplest rule, and I'm stealing it: use ===. Always. Unless you have a very specific reason not to. == is that friend who tries too hard — and in code, trying too hard creates bugs at 3 AM.

Patrick discovering that 0 == "0" is true. We've all been Patrick. (via josefczech.cz)

The null circus

Okay. Remember null and undefined from day 2? The twins? Today they put on a whole circus. Real outputs from my practice file — I'm not making any of this up:

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

Equal... but not identical. Like celebrity lookalikes. The loose friend says "same thing!" The strict bouncer says "different species, get out."

But wait, it gets worse:

null == 0    // false
null > 0     // false
null >= 0    // true?!

Read that again slowly. null is NOT equal to 0. null is NOT greater than 0. But null IS greater-than-or-equal to 0.

Pick a lane, null.

The explanation (because I needed one too): >= secretly converts null to 0 first, so it becomes 0 >= 0 — true. But == has a special rule: null only loosely equals undefined, nothing else. So null == 0 stays false. It's not random. It's just... rules written by different people who never met each other.

And undefined? undefined looked at all of this and said "nah, I'm out":

undefined == 0   // false
undefined < 0    // false
undefined > 0    // false
undefined <= 0   // false
undefined >= 0   // false

ALL false. undefined simply refuses to participate in mathematics. Honestly? Respect.

NaN: the identity crisis

NaN == NaN   // false

NaN isn't even equal to ITSELF. Imagine looking in the mirror and the mirror goes "nah, that's not you." That's NaN's entire life. The ultimate identity crisis.

Oh, and one more — chains betray you:

123 == "123" == 123   // false?!

JavaScript reads left to right: 123 == "123" becomes true (loose friend converts). Then true == 123 becomes false (true becomes 1, and 1 is not 123). The chain breaks at the last link. I hate it here.

The decision makers

After all that chaos, logical operators felt like a cold drink of water. These are the symbols that make DECISIONS:

age < 18 && money > 200   // BOTH must be true
age > 10 || money > 200   // ONE true is enough
!(age > 10)               // the contrarian — flips it

&& is a door with two locks — you need both keys. || is a door where either key works. And ! just disagrees with everything. We all know someone like !.

You'll use these everywhere. Every "if this AND that" in every app ever — that's && doing its job.

Bonus: binary wizardry

And then, at the end of class, sir casually dropped bitwise operators on us. These work on the actual 1s and 0s:

4 & 5    // 4
11 | 14  // 15
5 ^ 7    // 2

But the cool ones are the shifters:

5 << 3    // 40

Take 5, which is 101 in binary. Shove all the bits 3 places left: 101000. That's 40. Shifting left = multiplying by 2, three times. And 20 >> 2 = 5 — shove right = divide.

My notes literally have the binary written out: 101 becomes 101000. Chef's kiss. I felt like a hacker again.


Sir left us homework: figure out what undefined != null gives. I'm not telling you. Go try it in your console — that's the whole point of this series. We learn by breaking things.

Tomorrow: wherever day 5 takes us. See you there.

I

Nice write-up, Day 4 is where JavaScript stops feeling friendly. The habit that saved me was using === everywhere and treating == as something to read, not write, with one exception some people keep: value == null to catch both null and undefined. NaN !== NaN still surprises people years in, which is why Number.isNaN and Object.is exist. Which comparison result surprised you the most so far?

iin1005ev