React Native/Javascript - How to know that object does not have the property?

Viewed 377

Let say :

myVariable.color;
myVariable.height;
myVariable.width;

But sometimes, myVariable only have "color" and "height" property.

I have try this, but no luck :

if(myVariable.width == undefined) {
//but not filtered here
}

How to know if myVariable doesn't contain "width" property by code, is it possible ?

2 Answers

You could try to double negate:

if(!!myVariable.width){
    //do something here
}

You are looking for hasOwnProperty.

If you would like to perform a search in the whole potential prototype chain of an object, you can also use the in operator.

if (width in object) {
Related