You want to do the check for undefined first. If you do it the other way round, it will generate an error if the array is undefined.

if (array === undefined || array.length == 0) {
    // array does not exist or is empty
}

Update

This answer is getting a fair amount of attention, so I'd like to point out that my original answer, more than anything else, addressed the wrong order of the conditions being evaluated in the question. In this sense, it fails to address several scenarios, such as null values, other types of objects with a length property, etc. It is also not very idiomatic JavaScript.

The foolproof approach
Taking some inspiration from the comments, below is what I currently consider to be the foolproof way to check whether an array is empty or does not exist. It also takes into account that the variable might not refer to an array, but to some other type of object with a length property.

if (!Array.isArray(array) || !array.length) {
  // array does not exist, is not an array, or is empty
  // β‡’ do not attempt to process array
}

To break it down:

  1. Array.isArray(), unsurprisingly, checks whether its argument is an array. This weeds out values like null, undefined and anything else that is not an array.
    Note that this will also eliminate array-like objects, such as the arguments object and DOM NodeList objects. Depending on your situation, this might not be the behavior you're after.

  2. The array.length condition checks whether the variable's length property evaluates to a truthy value. Because the previous condition already established that we are indeed dealing with an array, more strict comparisons like array.length != 0 or array.length !== 0 are not required here.

The pragmatic approach
In a lot of cases, the above might seem like overkill. Maybe you're using a higher order language like TypeScript that does most of the type-checking for you at compile-time, or you really don't care whether the object is actually an array, or just array-like.

In those cases, I tend to go for the following, more idiomatic JavaScript:

if (!array || !array.length) {
    // array or array.length are falsy
    // β‡’ do not attempt to process array
}

Or, more frequently, its inverse:

if (array && array.length) {
    // array and array.length are truthy
    // β‡’ probably OK to process array
}

With the introduction of the optional chaining operator (Elvis operator) in ECMAScript 2020, this can be shortened even further:

if (!array?.length) {
    // array or array.length are falsy
    // β‡’ do not attempt to process array
}

Or the opposite:

if (array?.length) {
    // array and array.length are truthy
    // β‡’ probably OK to process array
}
Answer from Robby Cornelissen on Stack Overflow
Top answer
1 of 1
1550

You want to do the check for undefined first. If you do it the other way round, it will generate an error if the array is undefined.

if (array === undefined || array.length == 0) {
    // array does not exist or is empty
}

Update

This answer is getting a fair amount of attention, so I'd like to point out that my original answer, more than anything else, addressed the wrong order of the conditions being evaluated in the question. In this sense, it fails to address several scenarios, such as null values, other types of objects with a length property, etc. It is also not very idiomatic JavaScript.

The foolproof approach
Taking some inspiration from the comments, below is what I currently consider to be the foolproof way to check whether an array is empty or does not exist. It also takes into account that the variable might not refer to an array, but to some other type of object with a length property.

if (!Array.isArray(array) || !array.length) {
  // array does not exist, is not an array, or is empty
  // β‡’ do not attempt to process array
}

To break it down:

  1. Array.isArray(), unsurprisingly, checks whether its argument is an array. This weeds out values like null, undefined and anything else that is not an array.
    Note that this will also eliminate array-like objects, such as the arguments object and DOM NodeList objects. Depending on your situation, this might not be the behavior you're after.

  2. The array.length condition checks whether the variable's length property evaluates to a truthy value. Because the previous condition already established that we are indeed dealing with an array, more strict comparisons like array.length != 0 or array.length !== 0 are not required here.

The pragmatic approach
In a lot of cases, the above might seem like overkill. Maybe you're using a higher order language like TypeScript that does most of the type-checking for you at compile-time, or you really don't care whether the object is actually an array, or just array-like.

In those cases, I tend to go for the following, more idiomatic JavaScript:

if (!array || !array.length) {
    // array or array.length are falsy
    // β‡’ do not attempt to process array
}

Or, more frequently, its inverse:

if (array && array.length) {
    // array and array.length are truthy
    // β‡’ probably OK to process array
}

With the introduction of the optional chaining operator (Elvis operator) in ECMAScript 2020, this can be shortened even further:

if (!array?.length) {
    // array or array.length are falsy
    // β‡’ do not attempt to process array
}

Or the opposite:

if (array?.length) {
    // array and array.length are truthy
    // β‡’ probably OK to process array
}
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί check-if-javascript-array-is-empty-or-not-with-length
How to Check if a JavaScript Array is Empty or Not with .length
October 5, 2020 - By Madison Kanna When you're programming in JavaScript, you might need to know how to check whether an array is empty or not. To check if an array is empty or not, you can use the .length property. The length property sets or returns the number ...
🌐
Ash Allen Design
ashallendesign.co.uk β€Ί blog β€Ί how-to-check-if-an-array-is-empty-in-javascript
How to Check If an Array Is Empty in JavaScript
January 8, 2024 - So if the array is empty, the length property will return 0. We can then check against this integer to determine whether the array is empty or not. If the array is empty, the expression will return true like so: ... There are some caveats to ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί javascript β€Ί check-if-an-array-is-empty-or-not-in-javascript
Check if an array is empty or not in JavaScript - GeeksforGeeks
July 11, 2025 - let a = []; if (a.length == 0) { console.log("Empty"); } else { console.log("Not Empty"); } The Array.isArray() method checks whether the given variable consist of array or not. if it returns true then is checks for the length and prints the ...
🌐
Reddit
reddit.com β€Ί r/learnjavascript β€Ί !! vs ==0 when checking if array is empty
r/learnjavascript on Reddit: !! vs ==0 when checking if array is empty
June 30, 2024 -

I have an array in a function and I want the function to return true/false depending on if the array is empty (return true if not empty and vice versa)

I have narrowed down the condition to these 2 possible return statements. Which one is preferred?

return result.recordset.length == 0

return !!result.recordset.length
🌐
CoreUI
coreui.io β€Ί blog β€Ί how-to-check-if-an-array-is-empty-in-javascript
How to check if an array is empty in JavaScript? Β· CoreUI
February 7, 2024 - Invoke a function with array spread and check arguments length: Use a function call with spread syntax to check if any arguments were passed. const isEmpty = array => ((...args) => args.length === 0)(...array) Mastering array manipulation, including accurately identifying empty arrays, is a foundational skill in JavaScript programming.
🌐
Mairo
blog.mairo.eu β€Ί 5-ways-to-check-if-javascript-array-is-empty
5 ways to check if Javascript Array is empty
March 26, 2022 - Speaking about the operators, I'd like to mention also the Nullish coalescing operator (??). Our array contains also an object, so we could check its property. let myArray = [1, 245, 'apple', { type: 'fruit' }] myArray?.[3]?.type ?? 'No type property' // 'fruit' We get whatever we evaluate on the left side if it's true, otherwise we get what is on the right side of ??. It depends on what you really need, but here's another way. Logical not operator negates the values. The following would return true in case myArray is empty, that is [], or undefined or null.
🌐
Flexiple
flexiple.com β€Ί javascript β€Ί check-if-array-empty-javascript
How to check if an array is empty using Javascript? - Flexiple
Discover how to easily check if an array is empty using JavaScript. Our concise guide provides step-by-step instructions for efficient array handling.
Find elsewhere
🌐
Flexiple
flexiple.com β€Ί javascript β€Ί javascript-check-if-array-is-empty
JavaScript Check If Array Is Empty - Flexiple
Logic Flow: Ensuring data is available before processing maintains logical consistency in your code. The simplest and most common method to check if an array is empty is by using the `length` property.
Top answer
1 of 16
706
if (typeof image_array !== 'undefined' && image_array.length > 0) {
    // the array is defined and has at least one element
}

Your problem may be happening due to a mix of implicit global variables and variable hoisting. Make sure you use var whenever declaring a variable:

<?php echo "var image_array = ".json_encode($images);?>
// add var  ^^^ here

And then make sure you never accidently redeclare that variable later:

else {
    ...
    image_array = []; // no var here
}
2 of 16
354

To check if an array is either empty or not

A modern way, ES5+:

if (Array.isArray(array) && array.length) {
    // array exists and is not empty
}

An old-school way:

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

A compact way:

if (typeof array != "undefined" && array != null && array.length != null && array.length > 0) {
    // array exists and is not empty
}

A CoffeeScript way:

if array?.length > 0

Why?

Case Undefined
Undefined variable is a variable that you haven't assigned anything to it yet.

let array = new Array();     // "array" !== "array"
typeof array == "undefined"; // => true

Case Null
Generally speaking, null is state of lacking a value. For example a variable is null when you missed or failed to retrieve some data.

array = searchData();  // can't find anything
array == null;         // => true

Case Not an Array
Javascript has a dynamic type system. This means we can't guarantee what type of object a variable holds. There is a chance that we're not talking to an instance of Array.

supposedToBeArray =  new SomeObject();
typeof supposedToBeArray.length;       // => "undefined"

array = new Array();
typeof array.length;                   // => "number"

Case Empty Array
Now since we tested all other possibilities, we're talking to an instance of Array. In order to make sure it's not empty, we ask about number of elements it's holding, and making sure it has more than zero elements.

firstArray = [];
firstArray.length > 0;  // => false

secondArray = [1,2,3];
secondArray.length > 0; // => true
🌐
Latenode
community.latenode.com β€Ί other questions β€Ί javascript
How can I verify if an array is empty or undefined? - JavaScript - Latenode Official Community
October 10, 2024 - What is an efficient method to determine if an array is either empty or undefined? Is this a viable way to check this? if (!array || array.length === 0) { // array is empty or doesn't exist }
🌐
Elightwalk
elightwalk.com β€Ί home
Easy Ways to Check If an Array is Empty in JavaScript
July 31, 2025 - To Check Empty Array in JavaScript, use the length property with the === operator. The length property indicates how many items exist in the array. If it's empty, the length will be 0, so you can check if the length is exactly 0 to see if the ...
Price Β  $
Call Β  +917600897405
Address Β  611, Shivalik Square, near Adani CNG pump, Ramapir Thekra, Nava Vadaj, 380027, Ahmedabad
🌐
Educative
educative.io β€Ί answers β€Ί how-to-check-if-an-array-is-empty-in-javascript
How to check if an array is empty in JavaScript
In JavaScript, arrays can be created using the square brackets [] notation or by using the array constructor new Array(). The square bracket notation is preferred. For example, the following piece of code creates an array of strings: const coursesArray = ["Cracking the Coding Interview", "System Design Demystified"] ... Let's understand the length property to check the emptiness of the array.
🌐
Favtutor
favtutor.com β€Ί articles β€Ί check-javascript-array-empty
Check if JavaScript Array is Empty or Not (with code)
January 22, 2024 - //declaring an empty array let arr=[1,2,3]; //checking the length if(arr.length==0){ console.log("The array is empty"); } else{ console.log("The array is not empty"); } ... In this example, we have taken an array with three elements.
🌐
Javatpoint
javatpoint.com β€Ί javascript-function-to-check-array-is-empty-or-not
JavaScript function to check array is empty or not - Javatpoint
JavaScript function to check array is empty or not - JavaScript function to check array is empty or not with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, objects, map, typedarray etc.
🌐
QuickRef.ME
quickref.me β€Ί home β€Ί how to check if an array is empty in javascript - quickref.me
How to check if an array is empty in JavaScript - QuickRef.ME
In this Article we will go through how to check if an array is empty only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.
🌐
Board Infinity
boardinfinity.com β€Ί blog β€Ί how-to-quickly-check-if-an-array-is-empty-or-not-in-javascript
Check if Array is Empty or Not-Javascript | Board Infinity
July 9, 2023 - The array.length property can be used to determine whether the array is empty. The array's element count is returned by this attribute. The integer evaluates to true if it is bigger than 0. When used with the AND(&&) operator, this method and ...
🌐
Medium
frontendinterviewquestions.medium.com β€Ί how-to-check-if-an-array-is-empty-in-javascript-778c7b727f68
How to Check if an Array is Empty in JavaScript? | by Pravin M | Medium
August 10, 2024 - The Array.isArray() method can be used in combination with the length property to perform this check. let arr = []; if (Array.isArray(arr) && arr.length === 0) { console.log("The array is empty."); } else {…
🌐
Quora
quora.com β€Ί How-do-you-check-if-the-array-is-empty-or-does-not-exist-in-JavaScript
How to check if the array is empty or does not exist in JavaScript - Quora
Answer: We can check whether the ... myArray = ["Horses", "Dog", "Cats"]; if(myArray.length === 0) { print("Array is empty!"); } else { print("Array has "+ myArray.length + " elements"); } Output......