How to Check a Variable is Undefined in React Native
While developing mobile apps with react native, chances are high that you may come across some scenarios where the value of the variable you declared becomes undefined. So, how to check if a variable is undefined or not in react native as well as JavaScript?
Earlier I have written about how to check undefined property while iterating through JavaScript object. The solution here also is almost similar. You can use typeof JavaScript operator to check the data type of the variable.
The typeof returns data type, that is whether the variable is “undefined”, “object”, “boolean”, “string”, “number”, “function” etc. Hence you can use typeof to check undefined variables as given below:
if (typeof yourVariable === 'undefined'){
//the variable is undefined
} else {
//the variable is not undefined
}
That’s it. That’s how you check if a variable is undefined in React Native.