
🚫 Stop Writing Old JavaScript — ✅ Start Using Modern Built-in APIs (Part 2)
Missed Part 1? [ Read it here ] — covers structuredClone , Object.groupBy , AbortController , Promise.any , and more. 🧩 More Modern APIs You Should Already Be Using 1. Object.hasOwn() ❌ Old way if ( obj . hasOwnProperty ( " key " )) { ... } ⚠️ Why this is dangerous hasOwnProperty can be overridden on the object itself Breaks completely on null -prototype objects (e.g. Object.create(null) ) const obj = Object . create ( null ); obj . name = " Nagendra " ; obj . hasOwnProperty ( " name " ); // ❌ TypeError: obj.hasOwnProperty is not a function // Also risky: const user = { hasOwnProperty : () => true , // someone overrides it }; user . hasOwnProperty ( " anything " ); // true ❌ — lies to you ✅ Modern way Object . hasOwn ( obj , " key " ); 🧠 Real-world use cases // Safe check on any object const config = Object . create ( null ); // null-prototype object config . theme = " dark " ; Object . hasOwn ( config , " theme " ); // true ✅ Object . hasOwn ( config , " version " ); // false ✅ // Fil
Continue reading on Dev.to
Opens in a new tab



