How Testing Object for Properties works?

I cant understand how obj.hasOwnProperty works, i need to search the solved problem in google

function checkObj(obj, checkProp) {
	// Only change code below this line
	if (obj.hasOwnProperty(checkProp)) {
		return obj[checkProp];
	}
	else {
		return "Not Found";
	}
	// Only change code above this line
}

console.log(checkObj(1,0))

Challenge: Testing Objects for Properties

Link to the challenge:

First of all, when in doubt, check the docs. Seriously, professional devs do this all … day … long.

More broadly, in JS objects inherit a bunch of things. One thing that objects inherit when they are created is a method (a function built into the object) called “hasOwnProperty” that will tell you if that object has an added property by that name.

1 Like