I have a an object
me = {
name: "Mo",
age: 28,
}
And I want to see if this object has the property "height" for example. (which it doesn't) How can i do this? So for example if it has the property "height" I can give it a value of "5,7".
PLEASE NOTE: I don't want to check for the property VALUE(me.name) but for the property NAME.
Thank you.
You can use the
inoperator:Note that if the object does not have a property named "height", you can still add such a property:
That works whether or not the object had a "height" property previously.
There's also the
.hasOwnPropertymethod inherited from the Object prototype:The difference between that and a test with
inis that.hasOwnProperty()only returns true if the property is present and is present as a direct property on the object, and not inherited via its prototype chain.