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
}
๐ŸŒ
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 ...
๐ŸŒ
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 ...
๐ŸŒ
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 - Checking the array length in the if condition could be shortened to just !blogPost.length which will evaluate to true if the array is empty since 0 is falsy and gets inverted and coerced into a boolean.
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ check-if-array-empty-javascript
How to check if an array is empty using Javascript? - Flexiple
March 10, 2022 - Discover how to easily check if an array is empty using JavaScript. Our concise guide provides step-by-step instructions for efficient array handling.
๐ŸŒ
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.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array
Array - JavaScript | MDN
September 28, 2025 - What callbackFn is expected to return depends on the array method that was called. The thisArg argument (defaults to undefined) will be used as the this value when calling callbackFn. The this value ultimately observable by callbackFn is determined according to the usual rules: if callbackFn is non-strict, primitive this values are wrapped into objects, and undefined/null is substituted with globalThis.
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 }
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ every
Array.prototype.every() - JavaScript | MDN
September 28, 2025 - If such an element is found, every() immediately returns false and stops iterating through the array. Otherwise, if callbackFn returns a truthy value for all elements, every() returns true. Read the iterative methods section for more information about how these methods work in general. every acts like the "for all" quantifier in mathematics. In particular, for an empty array, it returns true.
๐ŸŒ
Unreal Engine
forums.unrealengine.com โ€บ development โ€บ programming & scripting
How to Check If Array Is Empty? - Programming & Scripting - Epic Developer Community Forums
July 15, 2014 - I am trying to do a conditional to check if an Array is empty. I used Length, but the UE4 engine crashes doing this. My conditional is if Length
๐ŸŒ
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 {โ€ฆ