Js The Weird Parts ~repack~ Online
const obj = { showThis: showThis }; obj.showThis(); // obj
console.log(0 == false); // true console.log("" == false); // true console.log(null == undefined); // true console.log(NaN == NaN); // false (yes, NaN is not equal to itself) The rule is simple: . It compares both value and type. The == operator tries to be "helpful" by converting types behind your back. That "help" is the source of countless bugs. NaN : The Loneliest Number Speaking of NaN (Not a Number), it has a personality disorder.
console.log(1 + "1"); // "11" (string) console.log(1 - "1"); // 0 (number) Why? Because + is overloaded. If either operand is a string, it prefers string concatenation. But - doesn’t have a string version, so it coerces everything to numbers. Fun, right? js the weird parts
Welcome to the weird parts. Let’s start with the most infamous party trick.
const bound = showThis.bind("hello"); bound(); // String {"hello"} const obj = { showThis: showThis }; obj
function oops() { accidentalGlobal = "I'm everywhere now"; } oops(); console.log(window.accidentalGlobal); // "I'm everywhere now" You didn't use var , let , or const ? Congratulations, you just polluted the global object. This is why we have linters and "use strict" . The this keyword is like a chameleon on caffeine. Its value depends entirely on how a function is called.
So next time you see [] + [] returning "" , don’t cry. Laugh. Then fix it. And remember: you’re not alone. What’s the weirdest JavaScript bug you’ve ever encountered? Hit reply or drop a comment—I’d love to compare war stories. That "help" is the source of countless bugs
And arrow functions? They don’t have their own this at all—they inherit from the surrounding scope. Arrays in JS are just objects with numeric keys and a special length property. That means you can do... questionable things.