E-mail: sale@obdtools.net
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.
const obj = { showThis: showThis }; obj.showThis(); // obj js the weird parts
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. function oops() { accidentalGlobal = "I'm everywhere now";
const bound = showThis.bind("hello"); bound(); // String {"hello"} The this keyword is like a chameleon on caffeine
Welcome to the weird parts. Let’s start with the most infamous party trick.
It gets weirder:
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?