How to Check undefined Property in React Native

Sometimes, you may want to check if a value is undefined or not in react native. The usual scenario you face undefined property is while iterating through a JavaScript object with invalid keys. This blog post helps you to check if a JavaScript object property is undefined or not.

In JavaScript, you can make use of typeof which returns a string and tells the type. The typeof returns the string ‘undefined’ when the value is not defined. Hence you can check the value is undefined or not with that string.

Please see the example given below:

const book = {
  title: 'ReactNativeForYou'
};

console.log(typeof book.author); //undefined

if (typeof book.author === 'undefined'){
//Do your things
}

In the example above, we have an object named a book with a property title. As it doesn’t have a property named author, typeof returns ‘undefined’ and you can use the string to do your actions.

I hope this blog post helps you to identify whether a property is undefined or not in React Native. Have any questions? Use the comment section below.

Similar Posts

One Comment

Leave a Reply