Prabakaran
Prabakaran

Reputation: 221

How to find whether the particular array variable is empty or not in JavaScript?

I have an array variable like Array[0], and now I need to check whether that Array[0] contains value or not inside of it. What should I do?

Upvotes: 1

Views: 365

Answers (4)

Manticore
Manticore

Reputation: 1284

Checks if that array exists and does something if not so.

if(!array[0])
{
  //do something
}

Upvotes: 1

user1140574
user1140574

Reputation:

//pass array variable to this function

function isEmpty(array){
   if(array.length == 0)
    return true;
   else
    return false; 
}

Upvotes: 1

Jørgen
Jørgen

Reputation: 9130

if(array[0] !== undefined){
 // array[0] is defined.
}

Upvotes: 1

Tadeck
Tadeck

Reputation: 137350

If you have an array and call it eg. list, then you can check if it has some elements in the following manner:

if (list.length){
    // has some elements (exactly list.length elements)
} else {
    // does not have any elements
}

See the following for details: Documentation on length property of Array objects in JavaScript

Upvotes: 3

Related Questions