Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To check array or not in typeScript, use Array.isArray() method it will true or false based on value we have provided, even it’s detect between object or instance of array.

Let’s see short example to use if...else with Array.isArray() to check array or not in typeScript.

if(Array.isArray(array)){

}

Today, I’m going to show you How do I check array or not in typeScript, as above mentioned, I’m going to use the above-mentioned if...else method with Array.isArray().

Let’s start today’s tutorial how do you check array or not in typeScript?

TypeScript check array or not example

Here, we will take string variable with some data and use if...else method with Array.isArray() to check array or not.

let arr: Array<string> = ["infinitbility"];

if(Array.isArray(arr)){
  console.log("arr is array")
} else {
  console.log("arr is not array")
}

interface user {
  id: number,
  name: string
}

let obj: user  = {
  id: 1,
  name: 'infinitbility'
};

if(Array.isArray(obj)){
  console.log("obj is array")
} else {
  console.log("obj is not array")
}

I hope it helps you, All the best đź‘Ť.