{"slug": "javascript-type-coercion-a-deep-dive-into-the-weirdness", "title": "JavaScript Type Coercion: A Deep Dive into the Weirdness", "summary": "A JavaScript tutorial warns that type coercion rules can cause silent bugs in AI workflows and LLM agent logic, recommending strict equality (===) and Object.is() to avoid implicit coercion. The post details common interview snippets showing how empty arrays and objects stringify differently, and notes that NaN is never equal to itself.", "body_md": "# JavaScript Type Coercion: A Deep Dive into the Weirdness\n\nJavaScript's type coercion rules are basically a minefield for anyone who hasn't memorized the ECMAScript spec. If you're prepping for a React or Node.js interview, you'll likely hit these \"predict the output\" questions because they separate people who just \"use\" JS from those who actually understand the engine.\n\n### 4. The\n\nIf you're building a complex AI workflow or writing custom LLM agent logic in JS, these coercion rules can cause silent bugs in your data parsing. Always use\n\nThe core mental model for the `+`\n\noperator is simple: it tries to convert both sides to primitives. If either side ends up as a string, it defaults to string concatenation. If not, it attempts numeric addition.\n\nHere is a practical tutorial on the most common traps.\n\n## The Coercion Cheat Sheet\n\nBefore diving into the snippets, keep these conversions in mind:\n\nStringifies to`[]`\n\n(empty array):`\"\"`\n\n, converts to number`0`\n\n.Stringifies to`{}`\n\n(empty object):`\"[object Object]\"`\n\n, converts to number`NaN`\n\n.Stringifies to`null`\n\n:`\"null\"`\n\n, converts to number`0`\n\n.Stringifies to`undefined`\n\n:`\"undefined\"`\n\n, converts to number`NaN`\n\n.\n\n## Common Interview Snippets\n\n### 1. Array Concatenation\n\n```\nconsole.log([] + []);\n```\n\n**Output:**`\"\"`\n\n(empty string)**Why:** Both arrays are converted to primitives via`.toString()`\n\n. Since `String([])`\n\nis `\"\"`\n\n, you get `\"\" + \"\"`\n\n, which is an empty string.### 2. Array vs Object\n\n```\nconsole.log([] + {});\n```\n\n**Output:**`\"[object Object]\"`\n\n**Why:**`String([])`\n\nis `\"\"`\n\nand `String({})`\n\nis `\"[object Object]\"`\n\n. Because one operand is a string, JS performs concatenation: `\"\" + \"[object Object]\"`\n\n.### 3. The Parentheses Trap\n\n```\nconsole.log({} + []);\nconsole.log(({} + []));\n```\n\n**Output:**`\"[object Object]\"`\n\nfor both.**Why:** Inside`console.log`\n\nor parentheses, `{}`\n\nis explicitly treated as an object literal. Both sides stringify and concatenate.### 4. The `eval`\n\n/ Bare Block Trap\n\n```\neval('{} + []');\neval('({} + [])');\n```\n\n**Output:**`0`\n\nthen `\"[object Object]\"`\n\n**Why:** In the first case, the engine sees`{}`\n\nas an empty code block, not an object. It ignores the block and evaluates `+ []`\n\n. The unary `+`\n\noperator converts `[]`\n\nto a number, which is `0`\n\n.## Equality Gotchas\n\nOne of the most frequent \"gotchas\" is how JS handles `NaN`\n\nand `null`\n\n.\n\nThis is`NaN === NaN`\n\n:`false`\n\n.`NaN`\n\nis the only value in JS not equal to itself. To check for it, use`Number.isNaN()`\n\nor`Object.is()`\n\n.Returns`typeof null`\n\n:`\"object\"`\n\n. This is a legacy bug from the first version of JS that was never fixed to avoid breaking the web.Even though they are \"empty,\" both are truthy.`[]`\n\nand`{}`\n\nTruthiness:\n\nIf you're building a complex AI workflow or writing custom LLM agent logic in JS, these coercion rules can cause silent bugs in your data parsing. Always use\n\n`===`\n\nto avoid implicit coercion and `Object.is()`\n\nwhen you need to detect `NaN`\n\n.[Next Union-Find: A Deep Dive into Disjoint Set Union →](/en/threads/3579/)\n\n## All Replies （3）\n\nD\n\nI always use strict equality (===) now to avoid those weird truthy/falsy bugs.\n\n0\n\nM\n\nspent way too many hours debugging\n\n`[] == ![]`\n\nback in the day. absolute nightmare.\n0\n\nZ\n\nDoes this still hold up with the newer nullish coalescing operator or is that different?\n\n0", "url": "https://wpnews.pro/news/javascript-type-coercion-a-deep-dive-into-the-weirdness", "canonical_source": "https://promptcube3.com/en/threads/3596/", "published_at": "2026-07-26 07:46:22+00:00", "updated_at": "2026-07-26 08:08:10.876968+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["JavaScript", "ECMAScript", "React", "Node.js"], "alternates": {"html": "https://wpnews.pro/news/javascript-type-coercion-a-deep-dive-into-the-weirdness", "markdown": "https://wpnews.pro/news/javascript-type-coercion-a-deep-dive-into-the-weirdness.md", "text": "https://wpnews.pro/news/javascript-type-coercion-a-deep-dive-into-the-weirdness.txt", "jsonld": "https://wpnews.pro/news/javascript-type-coercion-a-deep-dive-into-the-weirdness.jsonld"}}