DEV Community

Muhammad Mubashir
Muhammad Mubashir

Posted on

JavaScript is weird!

JavaScript is the only language where your brain melts in seconds.

So yesterday, I’m building some app, right? I’m checking types, doing calculations… and then I see this:

console.log(typeof NaN); // "number"

WTF JavaScript. NaN = Not a Number. Type = “number”? My brain just did a flip. How does this make sense?!

Then I try something else:

let a;
console.log(a + 2); // NaN
console.log(a); // undefined

Okay, so undefined + 2 = NaN. Makes sense… kinda. But typeof a is “undefined”. Sure, okay. But NaN is number? My brain just filed for early retirement.

I spend the next 20 minutes testing stuff:

console.log(null == undefined); // true
console.log(null === undefined); // false
console.log([] + []); // ""
console.log([] + {}); // "[object Object]"
console.log({} + []); // 0 ????

WHAT?! Why does JS do this to me? Why is [] + {} a string but {} + [] = 0???

At this point, I unlocked a new mental badge:

“JavaScript Survivor” — For staring at the console like it personally betrayed you.

Seriously, JS is fun… until it isn’t. But you still love it, because no other language would let typeof(NaN) === "number" and let you survive it.

Top comments (0)