DEV Community

loading...

Implementing custom `isFalsy()`

Mehul Lakhanpal
Full stack Developer | Micro-blogging on Web Development at codedrops.tech | Creator of File Ops, Note Box, GitHub Marker
Originally published at codedrops.tech ・1 min read
const isFalsy = (value) => {
  const isUndefined = value === undefined;
  const isNull = value === null;
  const isEmptyString = typeof value === "string" && !value.trim();
  const isEmptyArray =
    typeof value === "object" && Array.isArray(value) && 
        value.length === 0;
  const isEmptyObject =
    typeof value === "object" &&
    !Array.isArray(value) &&
    Object.keys(value).length === 0;

  return (
    isUndefined || isNull || isEmptyString || 
    isEmptyArray || isEmptyObject
  );
};
Enter fullscreen mode Exit fullscreen mode

Thanks for reading 💙

Follow @codedrops.tech for daily posts.

InstagramTwitterFacebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Discussion (0)