🧠 The Ultimate JavaScript Head-Scratcher: typeof null and Other Peculiarities! 🤯

Ankit kumar - Jun 20 - - Dev Community

Hey Devs!

Buckle up because today we’re diving into the wacky world of JavaScript quirks that will make you question your sanity, your code, and possibly your life choices. Let's decode some of the most bizarre, brain-twisting aspects of JavaScript that only the bravest dare to confront. 🚀

  • typeof null is object? Whaaat? 🤷‍♂️
console.log(typeof null); // 'object'

Enter fullscreen mode Exit fullscreen mode

Why? Because of reasons. Well, it’s a bug in JavaScript that's been around since day one, and fixing it now would break the web. 🤦

  • The Mystery of NaN (Not-a-Number)
console.log(NaN === NaN); // false

Enter fullscreen mode Exit fullscreen mode

That’s right. In the JavaScript universe, even NaN doesn’t equal NaN. It's like trying to compare apples and oranges, but both are on fire. 🔥🍏🔥🍊

  • The Schrodinger’s Cat of Arrays 🐱📦
const arr = [1, 2, 3];
arr.length = 0;
console.log(arr[0]); // undefined

Enter fullscreen mode Exit fullscreen mode

You just made the whole array disappear like a magic trick. Poof! 🎩✨

  • The Incredible Mutating const
const obj = { a: 1 };
obj.a = 2;
console.log(obj.a); // 2

Enter fullscreen mode Exit fullscreen mode

Surprise! const doesn’t make the object immutable. It just means you can’t reassign obj to something else, but you can still mutate its contents. Sneaky, right? 🕵️‍♀️

  • The Case of the Floating Decimal
console.log(0.1 + 0.2 === 0.3); // false

Enter fullscreen mode Exit fullscreen mode

Just when you thought you could trust math, JavaScript throws in some floating-point shenanigans to keep you on your toes. 🤡

  • The Time-Traveling Date
const date = new Date('2024-06-20');
date.setDate(date.getDate() - 1);
console.log(date.toString()); // Date in the previous month or year!

Enter fullscreen mode Exit fullscreen mode

Dates are like time machines in JavaScript. Change one thing, and who knows where you'll end up? 🕰️🔮

  • The Mystery of Array vs. Object
console.log([] instanceof Object); // true

Enter fullscreen mode Exit fullscreen mode

Arrays are just specialized objects in JavaScript. It's like finding out your cat is actually a small, furry object that meows. 🐈➡️📦

  • The Phantom undefined
let phantom;
console.log(phantom); // undefined
console.log(typeof phantom); // 'undefined'

Enter fullscreen mode Exit fullscreen mode

Undefined variables are a rite of passage in JavaScript. Embrace the void! 🌌

  • The Inescapable Infinity
console.log(1 / 0); // Infinity

Enter fullscreen mode Exit fullscreen mode

Divide by zero, and JavaScript just gives up and declares infinity. Simple as that. ♾️

  • The Quantum Leap of == and ===
console.log(0 == '0'); // true
console.log(0 === '0'); // false

Enter fullscreen mode Exit fullscreen mode

== and === are like parallel universes in JavaScript. One loves type coercion, and the other doesn’t even know what that means. 🌌🔗


JavaScript: Where rules are meant to be broken, and every quirk is a learning opportunity! 💡

Did you survive this trip down the rabbit hole? 🕳️🐇 Share your favorite (or most dreaded) JavaScript quirks below, and let’s revel in the madness together! 😜

Happy coding, folks! 🚀

. . . .