Home isEmpty Sample
Post
Cancel

isEmpty Sample

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const isEmpty = val => {
  if (val === undefined) return true;

  if (
    typeof val == 'function' ||
    typeof val == 'number' ||
    typeof val == 'boolean' ||
    Object.prototype.toString.call(val) === '[object Date]'
  ) {
    return false;
  }

  // null or 0 length array
  if (val == null || val.length === 0) {
    return true;
  }

  if (typeof val == 'object') {
    // empty object

    let r = true;

    for (const f in val) {
      r = false;
    }

    return r;
  }

  return false;
};

console.log(isEmpty()); // true
console.log(isEmpty(null)); // true
console.log(isEmpty(undefined)); // true
console.log(isEmpty('')); // true
console.log(isEmpty({})); // true
console.log(isEmpty([])); // true
This post is licensed under CC BY 4.0 by the author.