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 string ‘undefined’ when the value is not defined. Hence you can check the value 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 book with 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 blogpost helps you to identify a property is undefined or not in React Native. Have any questions? Use the comment section below.

1 thought on “How to Check undefined Property in React Native”

  1. Pingback: How to Check a Variable is Undefined in React Native - REACT NATIVE FOR YOU

Leave a Reply

%d bloggers like this: