Avatar ·

How to check if a key exists in a JavaScript object?

Hi everyone! I'm new to JavaScript and ran into a problem when working with objects. I have an object that contains user data: ```javascript const users = { 'user1': { name: 'Anna', age: 25 }, 'user2': { name: 'Ivan', age: 30 }, 'user3': { name: 'Maria', age: 28 } }; ``` I need to check if a specific key (e.g., 'user4') exists in this object before accessing its value. If I just write `users['user4']`, I get `undefined`, but this is not always safe — for example, if the key's value could be `null` or `0`, then checking for `undefined` will break. I tried using `if (users['user4'])`, but it doesn't work if the value is `false`, `0`, `""`, or `null`. I also read about `hasOwnProperty`, but I heard it has some issues.