Last active
May 9, 2020 05:49
-
-
Save SeanBannister/e80882d89f104a0b46020e310640ca4f to your computer and use it in GitHub Desktop.
Check if a variable/array/object is empty/null/undefined/etc in javascript.
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
function isEmpty(value) { | |
// If any of these return true this function returns true. | |
return ( | |
// Null or undefined. | |
(value == null) || | |
// Check if a Set() or Map() is empty | |
(value.size === 0) || | |
// NaN - The only JavaScript value that is unequal to itself. | |
(value !== value) || | |
// Length is zero && it's not a function. | |
(value.length === 0 && typeof value !== "function") || | |
// Is an Object && has no keys. | |
(value.constructor === Object && Object.keys(value).length === 0) | |
) | |
} | |
console.log( isEmpty(false) ); // False | |
console.log( isEmpty(true) ); // False | |
console.log( isEmpty(0) ); // False | |
console.log( isEmpty(1) ); // False | |
console.log( isEmpty(-1) ); // False | |
console.log( isEmpty(" ") ); // False | |
console.log( isEmpty("test") ); // False | |
console.log( isEmpty([0]) ); // False | |
console.log( isEmpty(['']) ); // False | |
console.log( isEmpty(['a','a']) ); // False | |
console.log( isEmpty({'':''}) ); // False | |
console.log( isEmpty({'a':'a'}) ); // False | |
console.log( isEmpty(new Object(false)) ); // False | |
console.log( isEmpty(new Object(true)) ); // False | |
console.log( isEmpty(new Object(0)) ); // False | |
console.log( isEmpty(new Set([0])) ); // False | |
console.log( isEmpty(new Map([['a', 1]])) ); // False | |
console.log( isEmpty(new WeakMap().set({}, 1)) ); // False | |
console.log( isEmpty(() => {}) ); // False | |
console.log( isEmpty(new RegExp()) ); // False | |
// Regex matching a blank space | |
console.log( isEmpty(/ /) ); // False | |
// Regex matching test | |
console.log( isEmpty(/test/) ); // False | |
var a = function () { return "test"; } | |
console.log( isEmpty(a) ); // False | |
var b = function () { return ""; } | |
console.log( isEmpty(b) ); // False | |
var c; | |
console.log( isEmpty(c) ); // True | |
console.log( isEmpty({}) ); // True | |
console.log( isEmpty([]) ); // True | |
console.log( isEmpty("") ); // True | |
var d = {'d':''}; | |
console.log( isEmpty(d.d) ); // True | |
console.log( isEmpty(d.d.d) ); // True | |
var e = ['']; | |
console.log( isEmpty(e[0]) ); // True | |
console.log( isEmpty(new Object('')) ); // True | |
console.log( isEmpty(null) ); // True | |
console.log( isEmpty(undefined) ); // True | |
console.log( isEmpty(NaN) ); // True | |
// Will also output NaN | |
console.log( isEmpty(parseInt('')) ); // True | |
// An empty regex | |
var f = //; | |
console.log( isEmpty(f) ); // True | |
console.log( isEmpty(new Set()) ); // True | |
console.log( isEmpty(new Set([])) ); // True | |
console.log( isEmpty(new Map()) ); // True | |
console.log( isEmpty(new Map([])) ); // True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment