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
}
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ check-if-array-empty-javascript
How to check if an array is empty using Javascript? - Flexiple
March 10, 2022 - The answer is NO, because an array in JavaScript is an instance of the Array object and typeof would return the type object for it.
Discussions

How can I verify if an array is empty or undefined?
How can I verify if an array is empty or undefined ยท What is an efficient method to determine if an array is either empty or undefined? Is this a viable way to check this ยท To determine whether an array is empty or undefined in JavaScript, an alternative approach can be considered by leveraging ... More on community.latenode.com
๐ŸŒ community.latenode.com
0
October 10, 2024
!! vs ==0 when checking if array is empty
Why is this downvoted? It's literally the 'learnjavascript' sub. More on reddit.com
๐ŸŒ r/learnjavascript
63
94
June 30, 2024
JavaScript WTF: Why does every() return true for empty arrays?
Because every member of the array meets the condition. It's logical More on reddit.com
๐ŸŒ r/javascript
40
0
September 5, 2024
Official/Standard way for checking if array is empty
The code of Symfony itself does not use empty to check for empty arrays, and I think the opinion there is to avoid empty wherever possible. The $array === [] is correct, and don't have the weird behavior of empty, if $array is not an array. There is also the count($array) === 0 check, which I think is the best option, as it also works to check if array-like objects are empty, as long as they implement countable (like doctrines collections or ArrayObject). Empty() will always return false for these array-like objects, as the object is non-null. More on reddit.com
๐ŸŒ r/PHP
89
54
April 17, 2024
๐ŸŒ
Bacancy Technology
bacancytechnology.com โ€บ qanda โ€บ javascript โ€บ check-if-array-is-empty-or-does-not-exist
How to Check If Array is Empty or Does Not Exist?
July 15, 2025 - Output: The array is empty. function isArrayEmpty(arr) { return Array.isArray(arr) && arr.length === 0; } // Usage console.log(isArrayEmpty([])); // true console.log(isArrayEmpty([1, 2, 3])); // false console.log(isArrayEmpty("Not array")); // false ยท Using Array.isArray() ensures the input is actually an array, avoiding errors if the input is something else like null or undefined. Work with our skilled Javascript developers to accelerate your project and boost its performance.
๐ŸŒ
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 ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array
Array - JavaScript | MDN
September 28, 2025 - Methods that have special treatment for empty slots include the following: concat(), copyWithin(), every(), filter(), flat(), flatMap(), forEach(), indexOf(), lastIndexOf(), map(), reduce(), reduceRight(), reverse(), slice(), some(), sort(), and splice(). Iteration methods such as forEach don't ...
๐ŸŒ
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 }
๐ŸŒ
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 - If the array is empty, the expression will return true like so: ... There are some caveats to using this approach, and we'll cover them further down in this article. A similar approach to the previous one is to use the length property with the ! operator. The ! operator is the logical NOT operator, and since in JavaScript 0 is a falsy value, we can use the !
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 simplest and most common way to check if an array is empty is by using the length property. The length property returns the number of elements in the array.
๐ŸŒ
DEV Community
dev.to โ€บ kontent_ai โ€บ how-to-check-if-array-is-empty-in-typescript-573k
How To Check If Array Is Empty In TypeScript - DEV Community
December 8, 2021 - Actually, it is. The API response is typed, so in case you get different type of data, it won't get mapped to the type and the items array will be empty which in the end helps you handle the problem.
๐ŸŒ
Favtutor
favtutor.com โ€บ articles โ€บ check-javascript-array-empty
Check if JavaScript Array is Empty or Not (with code)
January 22, 2024 - Letโ€™s look at methods to check whether an array is empty or not: We can use the .length property to check if a JavaScript array is empty or not. It can check the length of the array specifies the number of elements that it contains.
๐ŸŒ
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 - We started with the length property and introduced operators like &&, optional chaining .?, Nullish coalescing operator ?? and finally isArray() method. If you work with TypeScript, you could incorporate to your application also the snippet that checks for empty array.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ slice
Array.prototype.slice() - JavaScript | MDN
If end >= array.length or end is omitted or undefined, array.length is used, causing all elements until the end to be extracted. If end implies a position before or at the position that start implies, an empty array is returned.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_arrays.asp
JavaScript Arrays
JavaScript has a built-in array constructor new Array(). But you can safely use [] instead. These two different statements both create a new empty array named points:
๐ŸŒ
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
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ length
Array: length - JavaScript | MDN
When length is set to a bigger value than the current length, the array is extended by adding empty slots, not actual undefined values.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how do i empty an array in javascript?
How do I Empty an Array in JavaScript? | Sentry
December 15, 2022 - The variable arr2 does not copy the value of arr when itโ€™s assigned to it. It stores the reference to the array, which is its address in memory. You can set the length property of an array to 0 to empty an array:
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-clear-a-javascript-array-js-empty-array
How to Clear a JavaScript Array โ€“ JS Empty Array
June 27, 2022 - You can reassign a new value (an empty array) to a variable that initially has a non-empty array assigned to it.
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-empty-an-array-in-javascript
How to Empty an Array in JavaScript? - GeeksforGeeks
July 11, 2025 - // Given array let a = [ 10, 20, 30, 40, 50 ]; // Empty the array by setting its length to 0 a.length = 0; // Display Updated array and its length console.log("Updated Array: ", a); console.log("Length of Updated Array: ", a.length); ... The ...