
structuredClone, libraries, and DIY recipes—plus the pitfalls with Dates, Maps, Sets, typed arrays, class instances, circular refs, and performance. Introduction You copied an object, tweaked a nested field… and your UI mysteriously updated in the wrong place. Classic shallow copy trap. In JavaScript, most copies are shallow — they duplicate top-level properties but keep references to nested…

How to use computed property names, symbols, and expressions as object keys — safely and effectively in real-world code. Introduction Have you ever needed to create an object where the key comes from a variable instead of being hardcoded? const key = “status”;const obj = { [key]: “active” };console.log(obj); // { status: “active” } That’s dynamic object…

Turn objects into arrays of key–value pairs and unlock powerful patterns for iteration, transformation, and real-world dev workflows. Introduction Ever tried to loop through an object and ended up writing something clunky like this? for (const key in obj) { if (obj.hasOwnProperty(key)) { console.log(key, obj[key]); }} That’s old-school. Since ES2017, JavaScript gave us Object.entries—a clean,…

How to turn key–value pairs into objects, invert maps, filter entries, and unlock elegant patterns in modern JavaScript. Introduction You’ve probably used Object.entries to turn an object into an array of [key, value] pairs. But what about the reverse? That’s where Object.fromEntries comes in. Introduced in ES2019, it does the opposite of Object.entries: const entries…

A practical, senior-dev guide to ?., ?.(), and ?.[]—how to write safer JavaScript/TypeScript without the pyramid of &&, plus real-world patterns, React usage, and gotchas. Introduction We’ve all written this: const email = user && user.profile && user.profile.contact && user.profile.contact.email; It works… until you need to insert one more layer, then another. Readability tanks, bugs sneak in, and your…

Stop breaking on 0, ”, or false—why ?? is better than || for safe, predictable defaults in JavaScript and TypeScript. Introduction You’ve probably written this: const port = config.port || 3000; It looks fine until config.port = 0—a perfectly valid value—and your code “helpfully” replaces it with 3000. Oops. This is the classic falsy trap of…

Why and how to use Object.freeze to prevent mutations, what its limits are, and when to prefer alternatives like Object.seal, immutability patterns, or TypeScript. Introduction Have you ever debugged a bug that came from silent mutations? const config = { retries: 3 };config.retries = 99; // Oops, changed global config! In big apps — React, Redux, Node…

How to use get and set for clean APIs, validation, caching, and more—with real-world patterns in JavaScript and TypeScript. Introduction Most devs think of object properties as just “bags of values”: const user = { name: “Ali” };console.log(user.name); // “Ali” But JavaScript lets you define properties that run code when you access or assign them.…

A complete guide to iterating over objects in JavaScript — covering for…in, Object.keys, Object.entries, Object.values, and Maps—without the common mistakes. Introduction Looping through arrays in JavaScript is straightforward: for (const item of arr) { … } But with objects, things get messy. Should you use for…in, Object.keys, Object.entries, or for…of with a Map? Why does for…in sometimes…

How to check if an object really has a key — without prototype gotchas, null-prototype crashes, or security footguns. With modern Object.hasOwn, TypeScript patterns, and real-world examples. Introduction You need to know whether an object has a property. Most codebases have all three of these floating around: key in objobj.hasOwnProperty(key)Object.prototype.hasOwnProperty.call(obj, key) …and now there’s Object.hasOwn(obj, key), which…