Js The Weird: Parts

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

If you’ve spent more than five minutes writing JavaScript, you’ve probably had a moment where you stared at your screen and whispered, “...why?” console

const arr = [1, 2, 3]; arr["foo"] = "bar"; console.log(arr); // [1, 2, 3, foo: "bar"] console.log(arr.length); // 3 (still!) The length property only counts numeric indices. The string key "foo" is there, but the array pretends it isn’t. JavaScript tries to be "helpful" with Automatic Semicolon Insertion (ASI). But sometimes, it helps too much. That "help" is the source of countless bugs