Hi Friends 👋,
Welcome To Infinitbility! ❤️
To check array is undefined in typeScript, use if...else
statement it will handle all falsy value like undefined
, null
, empty
and etc.
Let’s see short example to use if...else
with array
to check array is undefined in typeScript.
if(array){
}
Today, I’m going to show you How do I check array is undefined in typeScript, as above mentioned, I’m going to use the above-mentioned if...else
statement.
Let’s start today’s tutorial how do you check array is undefined in typeScript?
TypeScript check array is undefined example
Here, we will take string variable with some data and use if...else
statement to check array is undefined.
let arr: Array<string> | undefined;
if(arr && arr.length){
console.log("arr is defined")
} else {
console.log("arr is undefined")
}
// You can also check specific conditions like a string is undefined or not
if(arr == undefined){
console.log("arr is undefined")
} else {
console.log("arr is defined")
}
I hope it helps you, All the best 👍.