Javascript Weird: Parts
Both get converted to strings and concatenated. Not an error. Not useful. Just... weird. JavaScript’s weird parts aren’t bugs—they’re historical artifacts. Brendan Eich built this language in 10 days in 1995. It had to be flexible, forgiving, and fast.
Use a small epsilon or multiply by powers of 10 for money calculations (or use BigInt for integers). 5. Automatic Semicolon Insertion (ASI) JavaScript tries to be helpful. Sometimes too helpful. javascript weird parts
console.log(1 + "2"); // "12" (string) console.log(1 + 2 + "3"); // "33" (evaluates left to right: 3 + "3") console.log([] + []); // "" (empty string) console.log([] + {}); // "[object Object]" console.log({} + []); // 0 (Wait, run this in a console... yes, 0) The last one is a parsing edge case where {} is interpreted as an empty code block, not an object. This one angers accountants and mathematicians equally. Both get converted to strings and concatenated
Let’s pop the hood and explore the —the quirks that make you scratch your head, and ultimately, make you a better developer. 1. typeof NaN === "number" (Excuse me?) You try to parse an integer from a string like "hello" . You get NaN (Not-a-Number). You ask JavaScript what type it is: Brendan Eich built this language in 10 days in 1995
function getObject() { return { value: 42 } } console.log(getObject()); // undefined