DEV Community

Cover image for 🧠 The Ultimate JavaScript Head-Scratcher: typeof null and Other Peculiarities! 🤯
Ankit kumar
Ankit kumar

Posted on

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

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! 🚀

Top comments (7)

Collapse
 
kooiinc profile image
KooiInc

If you think [] instanceof Object === true is weird, you do not understand ECMAScript I dare say.

What's the point of displaying these 'quirks' (some are, some aren't)? Most of it is old news, and it's mostly ruminated by people that don't like ECMAScript. Well, not liking ECMAScript is fine, but why the rumination? Don't use ECMAScript, find something else to program in, take your pick here.

Collapse
 
ankit_kumar_41670acf33cf4 profile image
Ankit kumar

Hey, thanks for sharing your thoughts! 👋

I appreciate your perspective. Indeed, many of these examples, like [] instanceof Object, reflect fundamental aspects of ECMAScript that seasoned developers understand well.

While some of these quirks are old news to experienced developers, discussing them serves multiple purposes. It helps newcomers, provides historical context, and fosters community engagement. And let’s be honest, sometimes it’s just plain fun to laugh at the peculiarities of the languages we work with every day. 😊

If you have more advanced or nuanced topics you'd like to discuss, I’d love to hear your thoughts! The beauty of our community is that we can learn from each other’s experiences and perspectives.

Thanks again for your comment.

Collapse
 
kooiinc profile image
KooiInc • Edited

Hi Ankit,

Ofcourse having a laugh every now and then is fine.

I would've preferred a more constructive description of quirks like this though (e.g. like this gist, or here).

I'd say: understanding what actually happens with such quirky snippets:

  • prevents developers to use weird and/or quirky constructs in their code
  • makes developers write better code

...and this is true for all programming languages.

Happy coding!

PS Much 'quirkiness' has to do with the (dynamic) ECMAScript type system, which imho has more advantages than disadvantages. For cases where type matters I created the module TypeofAnything. Maybe helpful to prevent quirkiness ;)

Thread Thread
 
ankit_kumar_41670acf33cf4 profile image
Ankit kumar

Hi @kooiinc ,

Thanks for your thoughtful reply! 😊

You make excellent points about the educational value of understanding what’s happening under the hood with these quirks. Providing deeper, constructive insights can indeed help developers avoid pitfalls and write better code. I appreciate the links you shared—they’re fantastic resources!

Thanks again for the constructive feedback. I’m always open to improving how we discuss and learn about JavaScript quirks. Sharing insights and tools is what makes our community strong and helps us grow as developers.

Happy coding, and keep up the great work! 🚀

Best,
Ankit

Collapse
 
ankit_kumar_41670acf33cf4 profile image
Ankit kumar • Edited
const pleaseLike = () => {
if(you_like){
console.log("please like,share and follow.")
}else{
console.log("please check more post of mine")
}
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kooiinc profile image
KooiInc • Edited

console.log(0.1 + 0.2 === 0.3); // false is not ECMAScript specific

Collapse
 
ankit_kumar_41670acf33cf4 profile image
Ankit kumar

Hey there! 👋

You’re absolutely right that the quirk with 0.1 + 0.2 !== 0.3 isn’t exclusive to JavaScript but is a broader issue with floating-point arithmetic in binary systems, impacting many languages, not just ECMAScript. This is a great point and helps in understanding that this behavior isn’t due to a JavaScript bug but rather how floating-point numbers are represented in computer systems.

Thanks