Use filter() to remove values below zero and then check if the length of resulting array is greater than or equal to two

const twoGreaterThanZero = arr => arr.filter(x => x > 0).length >= 2;

console.log(twoGreaterThanZero([9, 1, 0])) //true
console.log(twoGreaterThanZero([0, 0, 0])) //false
console.log(twoGreaterThanZero([5, 0, 0])) //false

Answer from Maheer Ali on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › length
Array: length - JavaScript - MDN Web Docs
const numbers = [1, 2, 3, 4, 5]; const length = numbers.length; for (let i = 0; i < length; i++) { numbers[i] *= 2; } // numbers is now [2, 4, 6, 8, 10] The following example shortens the array numbers to a length of 3 if the current length is greater than 3.
Discussions

check how many arrays have length greater than 0 in javascript and angularjs - Stack Overflow
I am trying to create a validation in my app. What I have, is multiple lists with a toggle (true/false). If the item is selected, it populates an array. And I have a different array for each gro... More on stackoverflow.com
🌐 stackoverflow.com
javascript - array.length vs. array.length > 0 - Stack Overflow
Is there any difference between checking an array's length as a truthy value vs checking that it's > 0? In other words is there any reason to use one of these statements over the other: var arr =... More on stackoverflow.com
🌐 stackoverflow.com
Could array.length be below 0 in Javascript? - Stack Overflow
2011-11-19 00:22:59 +00:00 Commented Nov 19, 2011 at 0:22 ... No, the length of an array is a non-negative integer. From the spec: Every Array has a non-configurable "length" property whose value is always a non-negative integral Number whose mathematical value is less than 2^32. ... So either check ... More on stackoverflow.com
🌐 stackoverflow.com
javascript - How to check if array is empty or does not exist? - Stack Overflow
What's the best way to check if an array is empty or does not exist? Something like this? if(array.length More on stackoverflow.com
🌐 stackoverflow.com
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript array length 0 | zero check and set array examples
JavaScript array length 0 | Zero Check and set Array examples
November 30, 2021 - Using Array length property can set or check array length 0 in JavaScript. This property returns the number of elements in the array. If the number is greater than 0, it evaluates to true.
🌐
xjavascript
xjavascript.com › blog › javascript-testing-length-and-length-0
JavaScript Testing: .length vs .length > 0 for Object Properties – Key Differences & Edge Cases Explained
.length > 0 is a boolean check that evaluates whether the .length of an object is greater than zero. It returns true if the object has elements (e.g., a non-empty array) and false otherwise (e.g., an empty array).
🌐
Codegive
codegive.com › blog › js_check_array_length.php
JS Check Array Length (2026): The Essential Guide to Mastering Array Sizes & Boosting Your Code Efficiency
Conditional Logic: You often need to perform actions only if an array contains elements, or a specific number of elements. Checking array.length allows you to implement such conditions reliably (e.g., if (array.length > 0)). Preventing Errors: Accessing an array element at an index equal to or greater than its length will result in undefined, not an error.
Find elsewhere
🌐
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 - Now we can check if the array is empty by using .length. ... This will return 0, as there are 0 items in the array.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript - MDN Web Docs
July 28, 2026 - In practice, such object is expected to actually have a length property and to have indexed elements in the range 0 to length - 1. (If it doesn't have all indices, it will be functionally equivalent to a sparse array.) Any integer index less than zero or greater than length - 1 is ignored when an array method operates on an array-like object.
Top answer
1 of 1
1559

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
}
🌐
Mimo
mimo.org › glossary › javascript › array-length
JavaScript Array Length: Master Data Handling
| | Check if Empty | if (arr.length === 0) | A simple and readable way to check if an array contains any elements before trying to process it. | | Truncate / Clear | arr.length = 0 | Setting the length to 0 is a quick way to empty an entire array. | You can set length to a lower value than the current length to truncate or clear an array.
🌐
Scaler
scaler.com › home › topics › javascript array length
Javascript Array Length Property - Scaler Topics
February 27, 2024 - Explanation: In the above code, ... in javascript using numbers.length property after that we have run a loop from 0 to len in which the loop in every iteration is decreasing the value of element by 1. At last after the loop completes we have printed the resulted array after the operation. ... In the above code, we have assumed an array of elements 11,12,13,14,15, and then we have imposed a condition in which it will check the length of the array. If it is greater than 4, it will ...
🌐
Dmitri Pavlutin
dmitripavlutin.com › the-magic-behind-array-length-property
The Magic Behind Array Length Property - Dmitri Pavlutin
January 17, 2016 - If using a number greater than the highest index (or using a number bigger than current length), the array will become sparse. It's rarely useful. ... It's possible to assign a different type than number to length. JavaScript will convert the primitive to a number. If the conversion result is NaN or number less than 0, an error is thrown Uncaught RangeError: Invalid array length.
🌐
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 - The ! operator is the logical NOT operator, and since in JavaScript 0 is a falsy value, we can use the ! operator to inverse the length property and see if the inversed property is truthy. If the array is empty, the length property will return 0 (which is falsy), so the expression will return true like so: ... If the array is not empty, the length property will return an integer greater than ...
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › javascript array length
JavaScript Array Length Property
November 4, 2024 - const fruits = ['Apple', 'Orange', 'Strawberry']; fruits.length = 0; console.log(fruits); // [] Copy ... If you set the length property of an array to a value lower than the highest index, all the elements whose index is greater than or equal to the new length are removed.
🌐
Soledadpenades
soledadpenades.com › posts › 2014 › why-i-check-for-length-0
Why I check for length === 0 | soledad penadés
December 25, 2014 - where if the todo.txt file can be opened, the file variable will hold a bigger than zero value representing the file descriptor, and hence it will be "true". But not everyone that comes to JavaScript has a C background, so I fear that each time I am "clever" and use one of these I am making it harder for new people to use or learn from my code.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Array.length is 0 but array is bigger than 0 - JavaScript - The freeCodeCamp Forum
February 7, 2018 - Tell us what’s happening: When I return noRepeat.length it is always 0 even though when I return noRepeat itself, the array returns just fine. Your code so far function permAlone(str) { var result = []; if (str.length == 1) { result.push(str); return result; } for (var i = 0; i