Created
November 17, 2020 19:55
-
-
Save DoctorDerek/513c7ccb117ecef1a946f58fa6a297dc to your computer and use it in GitHub Desktop.
Checking for an empty array with vanilla JavaScript and Array.isArray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const emptyArray = [] | |
const fullArray = [777] | |
const notAnArray = 777 | |
if (Array.isArray(emptyArray)) { | |
if (emptyArray.length > 0) { | |
console.log("Non-empty array") | |
} | |
} | |
if (Array.isArray(fullArray)) { | |
if (fullArray.length > 0) { | |
console.log("Non-empty array") | |
} | |
} | |
// Output: "Non-empty array | |
if (Array.isArray(notAnArray)) { | |
if (notAnArray.length > 0) { | |
console.log("Non-empty array") | |
} | |
} | |
// Using the logical AND operator && works the same as the last statement | |
// in that we will never try to access .length unless we get a value of | |
// true from Array.isArray | |
console.log(Array.isArray(emptyArray) && emptyArray.length > 0) // false | |
console.log(Array.isArray(fullArray) && fullArray.length > 0) // true | |
console.log(Array.isArray(notAnArray) && notAnArray.length > 0) // false | |
// You can create a simple function to find non-empty arrays: | |
const isNonEmptyArray = (maybeArray) => | |
Array.isArray(maybeArray) && maybeArray.length > 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment