JavaScript Array Reference Equality Explained

✅ JavaScript Output — Let’s Understand Why This Happens This morning’s code was: let a = []; let b = []; console.log(a == b); console.log(a === b); let c = a; console.log(a === c); 💡 Correct Output false false true  Simple Explanation : 🔹 Step 1: let a = []; let b = []; Even though both look the same ([]), JavaScript creates two different arrays in memory. So: a → one memory reference b → another memory reference 🔹 console.log(a == b); == checks value/reference. Since a and b point to different arrays, the result is: false ✔ Output: false 🔹 console.log(a === b); === checks value + type. But for objects/arrays, this really means: Are both variables pointing to the same memory location? They are not. So: false ✔ Output: false 🔹 let c = a; Now c points to the same array as a. So: a and c share the same memory reference 🔹 console.log(a === c); Now JavaScript checks: Same reference? → Yes So: true ✔ Output: true 🎯 Key Takeaways : Arrays and objects are reference types [] == [] and [] === [] are always false Two arrays with the same content are still different in memory Equality checks compare references, not structure 📌 That’s why in real code, we don’t compare arrays directly. 💬 Your Turn Did you already know this behavior? Comment “Knew it 👍” or “Learned today 😮” #JavaScript #FrontendDevelopment #LearnJS #CodingInterview #Arrays #TechWithVeera #WebDevelopment #100DaysOfCode Export Message as PDF

  • graphical user interface, application

Learned something new again today

Like
Reply

To view or add a comment, sign in

Explore content categories