Open In App

Check if an array is empty or not in JavaScript

Last Updated : 14 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

For doing any operation on the array it is important to have elements in an array. we can check the presence of elements in an array by calculating the length of that array, also there are many other ways to check whether the array is empty or not.

These are the following methods to Check if an Array is Empty or Not:

Method 1: Using array.isArray() method and array.length property

  • Check array existence and type with Array.isArray().
  • Verify emptiness using array.length.
  • Combine both with && to ensure the array exists and is not empty.

Syntax:

Array.isArray(emptyArray) && emptyArray.length

Example: This example shows the above-explained approach.

Javascript
function checkArray() {
    // Input arrays
    let emptyArray = [];
    let nonExistantArray = undefined;
    let fineArray = [1, 2, 3, 4, 5];

    // Checking for type and array length
    if (Array.isArray(emptyArray) && emptyArray.length)
        output = true;
    else output = false;

    // Display output
    console.log("Output for emptyArray:" + output);

    // Checking for type and array length
    if (
        Array.isArray(nonExistantArray) &&
        nonExistantArray.length
    )
        output = true;
    else output = false;
    // Display output
    console.log("Output for nonExistantArray:" + output);

    // Checking for type and array length
    if (Array.isArray(fineArray) && fineArray.length)
        output = true;
    else output = false;
    // Display output
    console.log("Output for fineArray:" + output);
}

// Function call
checkArray();

Output
Output for emptyArray:false
Output for nonExistantArray:false
Output for fineArray:true

Method 2: Checking the type and length of the array

  • Verify array existence by checking for ‘undefined’ or ‘null’ using the typeof operator.
  • Check if the array is empty by ensuring the existence of the array.length property and confirming it’s greater than 0.
  • Use the AND (&&) operator to ensure both array existence and non-emptiness.

Syntax:

typeof emptyArray != "undefined" && emptyArray != null && emptyArray.length != null
&& emptyArray.length > 0

Example: This example shows the use of the above-explained approach.

Javascript
function checkArray() {
    // Array inputs
    let emptyArray = [];
    let nonExistantArray = undefined;
    let fineArray = [1, 2, 3, 4, 5];

    // Checking array type and length
    if (
        typeof emptyArray != "undefined" &&
        emptyArray != null &&
        emptyArray.length != null &&
        emptyArray.length > 0
    )
        output = true;
    else output = false;

    // Display output
    console.log("Output for emptyArray:" + output);

    // Checking array type and length
    if (
        typeof nonExistantArray != "undefined" &&
        nonExistantArray != null &&
        nonExistantArray.length != null &&
        nonExistantArray.length > 0
    )
        output = true;
    else output = false;

    // Display output
    console.log("Output for nonExistantArray:" + output);

    // Checking array type and length
    if (
        typeof fineArray != "undefined" &&
        fineArray != null &&
        fineArray.length != null &&
        fineArray.length > 0
    )
        output = true;
    else output = false;

    // Display output
    console.log("Output for fineArray:" + output);
}
checkArray();

Output
Output for emptyArray:false
Output for nonExistantArray:false
Output for fineArray:true

Method 3: Using JavaScript Array.some() method

The Javascript arr.some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument method. 

Syntax:

arr.some(callback(element,index,array),thisArg)

Example: In this example, we will use JavaScript Array.some() method to check an empty array.

Javascript
function checkArray() {
    // Array inputs
    let emptyArray = [];
    let fineArray = [1, 2, 3, 4, 5];

    // Checking array
    let output = emptyArray.some((element) => true);
    // Display output
    console.log("Output for emptyArray:" + output);

    // Checking array
    output = fineArray.some((element) => true);
    // Display output
    console.log("Output for fineArray:" + output);
}
// Function call
checkArray();

Output
Output for emptyArray:false
Output for fineArray:true

Method 4: Using JavaScript toSrting() method

The JavaScript Array toString() Method returns the string representation of the array elements

Syntax:

arr.toString()

Example: In this article, we will use JavaScript toString() mehod to convert the given array to string and compare it with an empty string to give output.

Javascript
function checkArray() {
    // Array inputs
    let emptyArray = [];
    let fineArray = [1, 2, 3, 4, 5];

    // Checking array
    let output = emptyArray.toString() === "";
    // Display output
    console.log("Output for emptyArray:" + !output);

    // Checking array
    output = fineArray.toString() === "";
    // Display output
    console.log("Output for fineArray:" + !output);
}
// Function call
checkArray();

Output
Output for emptyArray:false
Output for fineArray:true

Method 5: Using JavaScript Array.every()

The JavaScript Array.every() method tests whether all elements in the array pass the test implemented by the provided function. This method returns true if the callback function returns true for every element in the array; otherwise, it returns false.

Syntax:

arr.every(callback(element, index, array), thisArg)

Example: In this example, we’ll use the Array.every() method to check if the array is empty.

JavaScript
function checkArray() {
    // Array inputs
    let emptyArray = [];
    let fineArray = [1, 2, 3, 4, 5];
 
    // Checking array
    let output = emptyArray.every((element) => false);
    // Display output
    console.log("Output for emptyArray:" + !output);
 
    // Checking array
    output = fineArray.every((element) => false);
    // Display output
    console.log("Output for fineArray:" + !output);
}
// Function call
checkArray();

Output
Output for emptyArray:false
Output for fineArray:true

Method 6: Using JSON.stringify():

Using JSON.stringify() to check if an array is empty involves converting the array to a JSON string and comparing it to ‘[]’. While it works, it’s inefficient and less readable compared to using `array.length === 0`.

Example: In this example The function isArrayEmpty uses array.length === 0 to check if an array is empty. It is efficient and readable. It correctly identifies emptyArray as empty and `nonEmptyArray` as not empty.

JavaScript
function isArrayEmpty(array) {
    return array.length === 0;
}

const emptyArray = [];
const nonEmptyArray = [1, 2, 3];

console.log("Is emptyArray empty?", isArrayEmpty(emptyArray)); 
console.log("Is nonEmptyArray empty?", isArrayEmpty(nonEmptyArray)); 

Output
Is emptyArray empty? true
Is nonEmptyArray empty? false

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads