DEV Community

Naveen Dinushka
Naveen Dinushka

Posted on

Check if an Object has a Property (freecodecamp notes)

So what's the easiest way to check if an object has a property.

Exercise

Finish writing the function so that it returns true if the object passed to it contains all four names, Alan, Jeff, Sarah and Ryan and returns false otherwise.

 let users = { Alan: { age: 27, online: true }, Jeff: { age: 32, online: true }, Sarah: { age: 48, online: true }, Ryan: { age: 19, online: true } }; function isEveryoneHere(userObj) { // Only change code below this line // Only change code above this line } 
Enter fullscreen mode Exit fullscreen mode

Answer

 function isEveryoneHere(userObj) { // Only change code below this line if (userObj.hasOwnProperty('Alan') && userObj.hasOwnProperty('Jeff') && userObj.hasOwnProperty('Sarah') && userObj.hasOwnProperty('Ryan')) { return true } else { return false } // Only change code above this line } 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)