JavaScript Type Coercion Interview Trick

🧠 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐓𝐲𝐩𝐞 𝐂𝐨𝐞𝐫𝐜𝐢𝐨𝐧 – 𝐓𝐡𝐞 𝐑𝐞𝐚𝐥 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐓𝐫𝐚𝐩 🚀 console.log([] + []); // "" console.log([] + {}); // "[object Object]" console.log({} + []); // "[object Object]" console.log(true + true); // 2 console.log("5" - 2); // 3 console.log("5" + 2); // "52" console.log(2 + "5"); // "25" console.log([1, 2] + [3, 4]); // "1,23,4" console.log(typeof NaN); // "number" console.log(1 < 2 < 3); // true let a; a++; console.log(a); // NaN 💡 What’s happening? + → string concatenation if any operand is string - → converts values to number Arrays/Objects → converted to strings true → 1, false → 0 NaN is still a number type ⚡ Tricky part: 1 < 2 < 3 → true < 3 → 1 < 3 → true 👉 JavaScript doesn’t behave randomly — it follows type coercion rules 🔥 Master this = Crack any frontend interview #JavaScript #FrontendDeveloper #TypeCoercion #CodingInterview #WebDevelopment #TechTips #LearnToCode #ReactJS #Hiring #FrontendRecruiter

👉 1 < 2 < 3 → true < 3 → 1 < 3 → true 1 < 2 → true true → converted to 1 1 < 3 → true 👉 JavaScript converts boolean → number, which makes this expression tricky! 👉 let a; a++ → undefined → NaN → result is NaN a starts as undefined a++ tries to convert it to a number → becomes NaN 👉 Any operation with undefined in numeric context results in NaN

Like
Reply

To view or add a comment, sign in

Explore content categories