JavaScript is the language we all love… and sometimes want to throw our keyboards at. It’s everywhere! From making your buttons dance to breaking your website at 2 AM for reasons no one can explain.
JavaScript is powerful, quirky, and downright weird. Let’s talk about some of the craziest things it does.
1. “NaN”
Is a Number?
You read that right. “NaN”
stands for “Not a Number,”
yet JavaScript classifies it as a number. It’s like someone saying, “I’m not hungry… but let’s go eat.”
console.log(typeof NaN); // "number"
Why, JavaScript? Why?
2. Adding Arrays? Sure, Why Not.
What happens when you add two arrays? You’d think JavaScript would throw an error, right? Nope. It just… joins them into a string.
console.log([1, 2] + [3, 4]); // "1,23,4"
This isn’t addition; this is nonsense. But hey, that’s JavaScript for you.
3. True + True = 2
?
Try this in your console:
console.log(true + true); // 2
Yep. Because true is treated as 1 and JavaScript thinks, "Math makes sense here!" It doesn’t, but let’s pretend it does.
4. The Mysterious undefined
and null
undefined
means something hasn’t been assigned a value. null
means it’s empty.
But are they the same? No.
console.log(undefined == null); // true
console.log(undefined === null); // false
Confused? So was I. And so is every new JavaScript developer.
5. The this
Problem
Ah, this
. The bane of JavaScript learners. In one context, this is an object. In another, it’s undefined
. In an arrow function? It’s something else entirely.
const obj = {
name: "JavaScript",
regular: function () {
console.log(this.name);
},
arrow: () => {
console.log(this.name);
},
};
obj.regular(); // "JavaScript"
obj.arrow(); //
Every time you think you understand this
, JavaScript pulls the rug out from under you.
6. Double Equals Is Lazy
In JavaScript, ==
doesn’t always care about type. So, it tries to convert things for you. That’s nice... until it isn’t.
console.log(0 == "0"); // true
console.log(0 == []); // true
console.log([] == ""); // true
Do yourself a favor: use ===
instead. Always.
7. Infinite Is a Number
What’s the biggest number in JavaScript? Infinity. What’s smaller than the smallest? Negative infinity. And yes, you can do math with them.
console.log(Infinity - Infinity); // NaN
console.log(Infinity > 1000000); // true
JavaScript is just casually flexing that math is relative.
Why We Love It Anyway
For all its quirks, JavaScript is… amazing. It lets you build entire applications, make websites interactive, and even control robots! It’s a little crazy, but that’s part of its charm.
JavaScript teaches us patience, makes us laugh (and cry), and, in the end, gets the job done. Embrace the weirdness.
Top comments (0)