JavaScript Array Comparison Explained

Yesterday poll answer and explanation console.log([2] == [2]); // false 👀 In JavaScript arrays are objects (reference types). So, when both sides are objects (reference types), JavaScript does not compare their values. Instead, it compares their memory references. console.log([2] == 2); // true 🤯 So here, left operand is an object (reference type) and right operand is a (primitive type) is number. In this case, type coercion happens. The reference type is first converted into a primitive. [2].toString() // "2" and it becomes "2" == 2 With ==, JavaScript converts string to number: Number("2") == 2, It becomes 2 == 2 So the final output is, false, true Hope this explanation is helpful to someone 😊 #JavaScript #FrontendDeveloper #WebDevelopment #LearningJavaScript #InterviewPrep

To view or add a comment, sign in

Explore content categories