Objects vs Sets in JavaScript Equality Check

Why does this return true… but this returns false? 🤔 const obj = { 1: 'a', 2: 'b', 3: 'c' }; const set = new Set([1, 2, 3, 4, 5]); obj.hasOwnProperty('1'); // true obj.hasOwnProperty(1); // true set.has('1'); // false set.has(1); // true Here’s the catch 👇 👉 Objects convert keys to strings 👉 Sets use strict equality (===) So: 1 → '1' (in objects) ✅ '1' !== 1 (in sets) ❌ 💡 Objects coerce. Sets don’t. Did you know this before? Follow for more bite-sized JavaScript insights 🚀 #javascript #software engineering #frontend development

To view or add a comment, sign in

Explore content categories