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 😊.

Quick step-by-step summary (click to expand)
  1. What is Array in JavaScript. Read the ‘What is Array in JavaScript?’ section for the details and code.
  2. Different methods on how to check if an array is empty in JavaScript. Read the ‘Different methods on how to check if an array is empty in JavaScript?’ section for the details and code.
  3. How to check if element is present in JavaScript array. Read the ‘How to check if element is present in JavaScript array?’ section for the details and code.
  4. Conclusion. Read the ‘Conclusion’ section for the details and code.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment