How to check if array is empty or not in JavaScript? 6 Methods

In this article, we will delve into how to check if a JavaScript array is empty or not with our in-depth guide.

We have covered six (6) different methods for checking if an array is empty and if an element is present in JavaScript, including using the .length property, the Array.isArray() method, and more.

This tutorial includes detailed explanations and code examples to help you improve your coding skills and write more efficient code.

We can guarantee that this tutorial will help you master the art of checking if a JavaScript array is empty and if an element is present.

What is Array in JavaScript?

An array in JavaScript is a data structure that allows storing multiple values using a single variable name.

It acts as a specialized object capable of holding a collection of items and provides methods for commonly used array operations.

Arrays in JavaScript follow a zero-based indexing system, where the first element resides at index 0, the second at index 1, and so forth.

Accessing array elements is done by referencing their respective index numbers. For instance, if we have an array called myArray with elements [“apple”, “banana”, “cherry”], we can retrieve the first element by calling myArray[0], resulting in the value “apple”.

JavaScript arrays are dynamic and can accommodate various data types. Elements can be added or removed from an array using methods like push(), pop(), shift(), and unshift().

Additionally, numerous built-in methods, including slice(), splice(), map(), filter(), and more, exist to facilitate array manipulation.

Different methods on how to check if an array is empty in JavaScript?

Now, let’s see how we can check if array is empty in JavaScript using different methods.

Method 1: Use .length property

One easy way to determine if an array is empty is by using the length property.

The length property tells us how many elements are in the array. If the length is 0, it means the array is empty.

Here’s an example:

const myArray = [];
if (myArray.length === 0) {
    console.log("The array is empty");
} else {
    console.log("The array is not empty");
}

Output:

The array is empty

Here’s another example:

const myArray = ["Apple", "Banana", "Cherry"];
console.log(myArray.length);

As you can see, in this example we create an array called myArray with the elements “Apple”, “Banana”, and “Cherry.”

It then logs the length of the array to the console using the length property, which returns the number of elements in the array.

In this case, it would log 3 to the console, since there are 3 elements in the myArray array.

Output:

3

Method 2: Use Array.isArray() method

You can use the Array.isArray() method to check if a variable is an array, and then check its length to see if it’s empty.

Here’s an example:

const myArray = ["apple"];
if (Array.isArray(myArray) && myArray.length === 0) {
    console.log("The array is empty");
} else {
    console.log("The array is not empty or not an array");
}

Output:

The array is not empty or not an array

Method 3: Use a conditional statement

To check if an array is empty, you can simply use a conditional statement because an empty array is considered “false” in JavaScript.

Here’s an example:

const myArray = [];
if (!myArray.length) {
    console.log("The array is empty");
} else {
    console.log("The array is not empty");
}

Output:

The array is empty

Method 4: Use the Array.prototype.every() method

To check if a JavaScript array is empty, you can use the every() method. It checks if all the elements in the array meet a certain condition.

By using this method along with checking the array length, we can figure out if the array is empty.

Here’s an example:

const myArray = [];
if (myArray.every(() => false)) {
    console.log("The array is empty");
} else {
    console.log("The array is not empty");
}

Output:

The array is empty

Method 5: Use the Array.prototype.reduce() method

You can use the Array.prototype.reduce() method to combine all the elements of an array into one value.

However, if the array is empty, the method will cause an error. By checking for this error, you can determine if the array is empty.

Here’s an example:

const myArray = [];
try {
    myArray.reduce(() => {});
    console.log("The array is not empty");
} catch (error) {
    if (error instanceof TypeError) {
        console.log("The array is empty");
    }
}

Output:

The array is empty

Method 6: Use for loop

You can use a for loop to go through each item in an array. If the loop doesn’t run, it means the array is empty.

Here’s an example:

const myArray = [];
let isEmpty = true;
for (const element of myArray) {
    isEmpty = false;
    break;
}
if (isEmpty) {
    console.log("The array is empty");
} else {
    console.log("The array is not empty");
}

Output:

The array is empty

How to check if element is present in JavaScript array?

In JavaScript, you have various methods to check if an element is present in an array. Here are some commonly used ways:

1. Use Array.prototype.includes() method

The Array.prototype.includes() method is used to find out if a specific value is present in an array. It returns either true or false, indicating whether the value exists in the array or not.

Here’s an example:

const myArray = ["Apples", "Banana", "Cherry"];
const searchElement = "Apples";
if (myArray.includes(searchElement)) {
    console.log(`${searchElement} is present in the array`);
} else {
    console.log(`${searchElement} is not present in the array`);
}

Output:

Apples is present in the array

2. Use the Array.prototype.indexOf() method

The Array.prototype.indexOf() method tells you the first position of a specific element in an array. If the element is not found, it returns -1.

Here’s an example:

const myArray = ["Apples", "Banana", "Cherry"];
const searchElement = "Banana";
if (myArray.indexOf(searchElement) !== -1) {
    console.log(`${searchElement} is present in the array`);
} else {
    console.log(`${searchElement} is not present in the array`);
}

Output:

Banana is present in the array

3. Use for loop

You can also use a for loop to iterate over the elements of an array and check if the element you’re looking for is present.

Here’s an example:

const myArray = ["Apples", "Banana", "Cherry"];
const searchElement = "Cherry";
let isPresent = false;
for (const element of myArray) {
    if (element === searchElement) {
        isPresent = true;
        break;
    }
}
if (isPresent) {
    console.log(`${searchElement} is present in the array`);
} else {
    console.log(`${searchElement} is not present in the array`);
}

Output:

Cherry is present in the array

Conclusion

In conclusion, this article provides a comprehensive guide to check if a JavaScript array is empty or not.

We have covered six different methods for checking emptiness, along with example codes and output, to aid in understanding and improving coding skills.

We also provide solutions on how to check if an element is present in an array of JavaScript.

The article provides clear explanations and code examples to help you improve your coding skills and write more efficient code.

We are hoping that this article provides you with enough information that helps you understand the JavaScript check if an array is empty.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment