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.
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?
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.
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.
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.
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.
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.
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.
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.
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.
Master JavaScript's powerful splice method! Learn how to add, remove, and replace array elements effortlessly. Simplify your code with this ultimate guide.
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.
The above is the detailed content of razy Things In JavaScript: Love It or Hate It. For more information, please follow other related articles on the PHP Chinese website!