JS isArray | Check if Variable is Array | JavaScript


JavaScript isArray. Check if Variable is Array ?

Sometimes, you need to check a parsed JSON object or a variable to see if it’s an array before iteration or before any other manipulation.

You can use the Array.isArray method to check if a variable is an array.

let jsonData = {
	employees : [{
  	firstName : 'Sam'
  },{
  	firstName : 'Roy'
  },{
  	firstName : 'Som'
  }]
};

if(jsonData && Array.isArray(jsonData.employees) && jsonData.employees.length){
	// do something
}