DEV Community

Cover image for 7 Crazy Things In JavaScript: Love It or Hate It
Tajammal Mabool
Tajammal Mabool

Posted on

7 Crazy Things In JavaScript: Love It or Hate It

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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();   // 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

JavaScript is just casually flexing that math is relative.

How to Check if a Key Exists in an Object in JavaScript | Tajammal Maqbool

Have you ever been knee-deep in JavaScript code and wondered, How to check if a key exists? In this blog, 4 different methods are explained.

favicon tajammalmaqbool.com

Time Events in JavaScript: A Guide | Tajammal Maqbool

JavaScript is amazing, isn’t it? It makes websites interactive and alive. One of its coolest features is Time Events. Don’t worry if it sounds technical; I’ll break it down.

favicon tajammalmaqbool.com

JavaScript Splice - The Ultimate Array Method | Tajammal Maqbool

Master JavaScript's powerful splice method! Learn how to add, remove, and replace array elements effortlessly. Simplify your code with this ultimate guide.

favicon tajammalmaqbool.com

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)