frontend-challenges
frontend-challenges copied to clipboard
13 - IsEmpty
index.js
export function isEmpty(value) {
switch (true) {
case Array.isArray(value) && value.length > 0:
return false;
case value instanceof Map && value.size > 0:
return false;
case value instanceof Set && value.size > 0:
return false;
case typeof value === "object" && value !== null && Object.keys(value).length > 0:
return false;
default:
return true;
}
}
The questions needs you to understand how to check certain data types in javascript:
- Array can be checked with
Array.isArray(value). - Map and Set types can checked with
instanceof. - We can check for object with type
typeof === "object". The main caveat is thatnullvalues are also object in javascript. So we need to put additional check for null values.